Java Parse int Exception

Static method in parseInt is derived from package java.lang.integer.public static method int parseInt(String s) gives you NumberFormatException. This parses the argument that passed as String into decimal integer.

Java Parse int Exception

Static method in parseInt is derived from package java.lang.integer.public static method int parseInt(String s) gives you NumberFormatException. This parses the argument that passed as String into decimal integer.

Java Parse int Exception

Java Parse int Exception

     

Static method in parseInt is derived from package java.lang.integer.public static method int parseInt(String s) gives you NumberFormatException. This parses the argument that passed as String into decimal integer. Even the character present in the string should be decimal one, exclude the first character, that may be ASCII subtract sign '-' ('c9808') to indicate a negative number. In return you get a integer value, if the argument and the radix is 10  given to the argument parseInt(String s,int )method.

Understand with Example

We declare a Public class name' inputtest'.Inside the main static method parseInt( ) method is used to convert a string format into a numeric integer. The Parameter Used in parseInt(java lang.String,int )method are 

1) s- Stand for string passed as an argument in parseInt Method and string contain the integer.

2) radix-Return the integer present in argument in decimal.

 throws you Number Format Exception, if the string does not contain parsable integer.

   
     import java.io.*;

    public class mytest
{
    public static void main(String[] args) throws IOException
  
{
     String s;
     int i;
     System.out.print("Enter a integer : ");
     s = readString();
     i = Integer.parseInt(s);

}

    public static String readString() throws IOException
{
    String Line = "";
    int c;
    while (true)
{
     c = System.in.read();
     if(c ==20)
     break;
     Line += (char) c;
}
    return Line;
  
}

 

 

Output on Command Prompt


C:\Documents and Settings\Administrator>cd\

C:\>cd saurabh\

C:\saurabh>javac mytest.java

C:\saurabh>java mytest
Enter a integer: 7
"xception in thread "main" java.lang.NumberFormatException: For input string: "7
at java.lang.NumberFormatException.forInputString(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at inputtest.main(inputtest.java:12)