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
try
{
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.
Good explanation!
good explanation
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?
Write this and try.
int a = Integer.parseInt(sub1.getText().toString().trim());
System.out.println(“xyz”);
System.err.println(“xyz”);
what is difference between err & out?
At which condition ,will give different output ?
err and out are object of PrintStream class. Both are connected internally to OS output streams. That is, both will print at DOS prompt. Better and appropriate use of err is in catch block. But for no difference.
Both out and err are the objects of same PrintStream class. One is connected to the out stream of OS and the other the other is connected to the err stream of OS. Both streams carry data from CPU to command prompt. Generally err is used to display error message as in catch block etc. Read more this in System.out.println()?
Would you like also to know System.in?
Though iava.lang.RuntimeException is subclass of java.lang.Exception it is not treated as checked excepyion .why?
It is the code designers have written. For example, HTTP protocol is connectionless protocol. But HTTP is developed on TCP/IP protocol which is connection-oriented protocol. Like many examples are available.