NumberFormatException is an unchecked exception thrown by parseXXX() methods when they are unable to format (convert) a string into a number.

Sometimes, in Java coding, we get input (like from command-line arguments and text field) from the user in the form of string. To use the string in arithmetic operations, it must be converted (parsed) into data types. This is done by parseXXX() methods of wrapper classes.

Following is the hierarchy of NumberFormatException.

Object –> Throwable –> Exception –> RuntimeException –> NumberFormatException

Full hierarchy of exceptions is available at "Hierarchy of Exceptions – Checked/Unchecked Exceptions".

String str1= “10”;
System.out.println(str1); // prints 10

The str1 represents 10, but it is in the form of string. Printing is no problem, but is not compatible for arithmetic manipulations. Following statements converts str1 into int form.

int x = Integer.parseInt(str1);
System.out.println(x*x); // prints 100

parseInt() is a method of Integer class which formats string into int data type. This is fine and works well. But what about the following? Suppose the user enters ten instead of numeric value 10 (remember, user, who uses your software product, is always innocent as he is not computer background having MCA or B.Tech (CSC) like a clerk in a bank). Is it possible to parseInt() method to parse string ten? Not possible and being unable to format, it throws "NumberFormatException". Following program illustrates.

public class NFE
  public static void main(String args[])
    String str1= "10";
    int x = Integer.parseInt(str1);
    System.out.println(x*x);    // prints 100

      String str2= "ten";
      int y = Integer.parseInt(str2);
    catch(NumberFormatException e)
      System.err.println("Unable to format. " + e);


As str2 cannot be formatted into a number, the JVM throws NumberFormatException and we caught it in catch block. Handling exception is a robust way of developing code.

9 thoughts on “NumberFormatException”

  1. package com.sum;

    import android.os.Bundle;
    import android.annotation.SuppressLint;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;
    import android.widget.EditText;
    import android.widget.TextView;
    import android.widget.Toast;

    public class MainActivity extends Activity {
    EditText sub1;
    EditText sub2;
    EditText sub3;
    Button button1;
    private TextView result;
    int subj1;
    int subj2;
    int subj3;
    static int input;
    protected void onCreate(Bundle savedInstanceState) {
    button1 = (Button) findViewById(;
    sub1 = (EditText) findViewById(;
    sub2 = (EditText) findViewById(;
    sub3 = (EditText) findViewById(;
    result = (TextView)findViewById(;

    button1.setOnClickListener(new OnClickListener() {

    public void onClick(View view) {
    Toast.makeText(MainActivity.this, “SumResult”, Toast.LENGTH_SHORT).show();

    int a = Integer.parseInt(sub1.getText().toString());
    int b = Integer.parseInt(sub2.getText().toString());
    int c = Integer.parseInt(sub3.getText().toString());

    int sum = a + b + c ;


    in this , wen passing zero as a ip for any one field, it s showing numberformat exception , how can i resolve this exception?

  2. System.out.println(“xyz”);
    what is difference between err & out?
    At which condition ,will give different output ?

Leave a Comment

Your email address will not be published.