NumberFormatException

NumberFormatException


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.

NumberFormatException

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.

8 thoughts on “NumberFormatException

  1. spoorthi

    package com.sum;

    import android.os.Bundle;
    import android.annotation.SuppressLint;
    import android.app.Activity;
    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;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    button1 = (Button) findViewById(R.id.button_sumresult);
    sub1 = (EditText) findViewById(R.id.edit_sub1);
    sub2 = (EditText) findViewById(R.id.edit_sub2);
    sub3 = (EditText) findViewById(R.id.edit_sub3);
    result = (TextView)findViewById(R.id.button_sumresult);

    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 ;

    result.setText(String.valueOf(sum));

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

  2. Mahi

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

Leave a Reply

Your email address will not be published. Required fields are marked *