tags:

views:

202

answers:

5

Hi When I run the following code I am getting NumberFormatException can anybody help me out in debugging code.

import java.io.*;
public class Case1 {
   public static void main(String args[])
   {  
       char ch='y';int i=0;
       BufferedReader bf=new  BufferedReader(new InputStreamReader(System.in));
       System.out.println("ch before while:::"+ch);
       while(ch=='y'||ch=='Y'){
           try{

       System.out.println("Enter the option");
         i=Integer.parseInt(bf.readLine());
       System.out.println("after"+i);

  switch {
       case 1 ystem.out.println("1");   break;
       case 2 ystem.out.println("1"); break;
       }
       System.out.println("do u want to continue(Y/y");
       ch=(char)bf.read();
       System.out.println("ch after execution:::"+ch);


   }
           catch(NumberFormatException e){e.printStackTrace();}
           catch(IOException e){e.printStackTrace();}
       }

}}
A: 

Maybe because readline string is like '123\n'.

foret
No, because readLine() returns the line without line-termination-string: http://download.oracle.com/javase/6/docs/api/java/io/BufferedReader.html#readLine%28%29
Mnementh
+1  A: 
System.out.println("Enter the option");
         i=Integer.parseInt(bf.readLine());   

Problem is here.

You are reading some non numeric input and trying to parse it into int. Thats the exceptional case.

org.life.java
+2  A: 

This problem i have faced in my daytoday work. The bf.readLine() gives you the empty string("") or character values [A-Z].So do a precondition check like

    // To allow only Integer to be parsed.
      String  rawText = br.readLine().trim();
      if (    isNumeric (rawText)              // returns false for non numeric
           && rawText.matches("-?\\d+?")           // Only Integers.
         )  

Updates :

// isNumeric implementation Due credit to CraigTP

Please refer for brilliant logic

http://stackoverflow.com/questions/1102891/how-to-check-a-string-is-a-numeric-type-in-java

public static boolean isNumeric(String str) 
{ 
  NumberFormat formatter = NumberFormat.getInstance(); 
  ParsePosition pos = new ParsePosition(0); 
  formatter.parse(str, pos); 
  return str.length() == pos.getIndex(); 
} 
Suresh S
How will you implement the `isNumeric` method?
dogbane
this `isNumeric` method won't work in this case because it allows doubles too. For example, the string "5.5" will pass the `isNumeric` check, but throw a `NumberFormatException` on `Integer.parseInt`.
dogbane
A: 

I replaced the inputstream with a Scanner.

public class Case1 {


   public static void main(String args[])
   {  
       char ch='y';int i=0;
      Scanner s=new Scanner(System.in);
       System.out.println("ch before while:::"+ch);
       while(ch=='y'||ch=='Y'){


       System.out.println("Enter the option");
       System.out.flush(); 
       i=s.nextInt();

       System.out.println("after"+i);

  switch(i) {
       case 1:System.out.println("1");   break;
       case 2:System.out.println("1"); break;
       }
       System.out.println("do u want to continue(Y/N)");
       ch=(char)s.next().charAt(0);
       System.out.println("ch after execution:::"+ch);


   }


}}
Emil
A: 

We can only use Explicit Type Conversion for data types which are type compatible with each other. But as you are trying to do type conversion on

ch=(char)bf.read();

you are actually trying to cast an Integer as char(since return type of bf.read() is int). But Integer and char are not compatible, hence the error.

Logan