Learn how to Make Money Online | Free Tech Magazines
Go4Expert
Go4Expert RSS Feed

Go Back   Programming and SEO Forum >  Go4Expert > Queries and Discussion > Programming > Java

Reply Copy HTML to Clipboard  Copy BBCode to Clipboard  Add to del.icio.us  Add to Google  Digg it  Add to Yahoo !  Add to Windows Live  Add to Facebook  Add to StumbleUpon 
 
Bookmarks Thread Tools Search this Thread Display Modes
Old 01-22-2008, 12:19 PM   #1
Kailash
Go4Expert Member
 
Join Date: Jul 2007
Posts: 14
Thanks: 0
Thanked 0 Times in 0 Posts
Rep Power: 0
Kailash is on a distinguished road
Question

Why is it throwing "number format exception" ?


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(Num berFormatException.
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 shabbir; 01-22-2008 at 05:38 PM. Reason: Code block
Kailash is offline   Reply With Quote
Old 01-30-2008, 04:35 PM   #2
dontbugme
Banned
 
Join Date: Nov 2007
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Rep Power: 0
dontbugme is on a distinguished road

Re: Why is it throwing "number format exception" ?


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
dontbugme is offline   Reply With Quote
Old 01-30-2008, 06:16 PM   #3
pradeep
Team Leader
 
pradeep's Avatar
 
Join Date: Apr 2005
Location: Kolkata, India
Posts: 1,461
Thanks: 0
Thanked 19 Times in 16 Posts
Rep Power: 6
pradeep will become famous soon enough
Send a message via Yahoo to pradeep

Re: Why is it throwing "number format exception" ?


change br.readLine() to br.read();
__________________
Vote for the Most Entertaining Member of 2008

To err is human,to detect is divine!
pradeep is offline   Reply With Quote
Old 02-14-2008, 02:42 PM   #4
arbaz.it
Ambitious contributor
 
Join Date: Feb 2008
Posts: 104
Thanks: 0
Thanked 0 Times in 0 Posts
Rep Power: 2
arbaz.it is on a distinguished road

Re: Why is it throwing "number format exception" ?


hi can you please give some explanation for your code i needed some help
arbaz.it is offline   Reply With Quote
Reply Copy HTML to Clipboard  Copy BBCode to Clipboard  Add to del.icio.us  Add to Google  Digg it  Add to Yahoo !  Add to Windows Live  Add to Facebook  Add to StumbleUpon 


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes
Bookmarks

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads / Articles
Thread Thread Starter Forum Replies Last Post
the web application take Date formate different thanserver date format. ali80_dh ASP / ASP.NET 4 01-17-2008 05:52 PM
New ads format of adsense Adulu Site Monetization 1 09-08-2007 05:05 PM
Custom Date Format with SimpleDateFormat class pradeep Java 0 10-07-2006 03:00 PM

 

All times are GMT +5.5. The time now is 05:30 AM.