Why is it throwing "number format exception" ?

Discussion in 'Java' started by Kailash, Jan 22, 2008.

  1. Kailash

    Kailash New Member

    Joined:
    Jul 24, 2007
    Messages:
    14
    Likes Received:
    0
    Trophy Points:
    0
    Code:
    //WAP That convert integer value into binary,octal and hexdecimal.
    
    import java.io.*;
    
    class numconv
    {
     public static void main(String s[])throws IOException
       { 
         BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
    
         char ch='y'; 
         int opt,num;
         String bin,oct,hex;
    
    
         do
          {
    
           System.out.println("*******  1: Convert number to Binary format ********");
           System.out.println("*******  2: Convert number to Octal format *********");
           System.out.println("*******  3: Convert number to Hexadecimal format ***");
           
           System.out.print("\n\n Enter Option :");
           opt=Integer.parseInt(br.readLine());
    
           switch(opt)
             {
                case 1:
                        System.out.print("\n\nEnter Number :");
                        num=Integer.parseInt(br.readLine());
                        bin=Integer.toBinaryString(num);
                        System.out.println("\nBinary equivalent of "+num+" is "+bin);
                        break; 
    
                case 2:
                        System.out.print("\n\nEnter Number :");
                        num=Integer.parseInt(br.readLine());
                        oct=Integer.toOctalString(num);
                        System.out.println("\nOctal equivalent of "+num+" is "+oct);
                        break;  
    
                case 3:
                        System.out.print("\n\nEnter Number :");
                        num=Integer.parseInt(br.readLine());
                        hex=Integer.toHexString(num);
                        System.out.println("\nHexadecimal equivalent of "+num+" is "+hex);
                        break;
    
               
             }
    
           System.out.print("\n\n Enter your choice(y/n) :");
           ch=(char)br.read();
           
          }while(ch!='n');
    
       }
    }


    Here is the output


    ******* 1: Convert number to Binary format ********
    ******* 2: Convert number to Octal format *********
    ******* 3: Convert number to Hexadecimal format ***


    Enter Option :1


    Enter Number :5643

    Binary equivalent of 5643 is 1011000001011


    Enter your choice(y/n) :y

    Here, when i am entering 'y' or value other than 'n' it is throwing exception.

    ******* 1: Convert number to Binary format ********
    ******* 2: Convert number to Octal format *********
    ******* 3: Convert number to Hexadecimal format ***


    Enter Option :Exception in thread "main" java.lang.NumberFormatException: For i
    nput string: ""
    at java.lang.NumberFormatException.forInputString(NumberFormatException.
    java:48)
    at java.lang.Integer.parseInt(Integer.java:447)
    at java.lang.Integer.parseInt(Integer.java:476)
    at numconv.main(numconv.java:24)
     
    Last edited by a moderator: Jan 22, 2008
  2. dontbugme

    dontbugme New Member

    Joined:
    Nov 27, 2007
    Messages:
    5
    Likes Received:
    1
    Trophy Points:
    3
    Gender:
    Male
    you type a letter "y", and then you type a line return

    read() just reads one character ("y")

    the line return is still in the stream

    then when readLine() is called, it reads until it encounters a line return, which is the first character it encounters

    so it returns an empty line

    Integer.parseInt() cannot parse an empty string
     
    shabbir likes this.
  3. pradeep

    pradeep Team Leader

    Joined:
    Apr 4, 2005
    Messages:
    1,645
    Likes Received:
    87
    Trophy Points:
    0
    Occupation:
    Programmer
    Location:
    Kolkata, India
    Home Page:
    http://blog.pradeep.net.in
    change br.readLine() to br.read();
     
  4. arbaz.it

    arbaz.it New Member

    Joined:
    Feb 13, 2008
    Messages:
    104
    Likes Received:
    0
    Trophy Points:
    0
    hi can you please give some explanation for your code i needed some help
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice