2

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();}
       }

}}
Benoit Courtine
  • 7,014
  • 31
  • 42
giri
  • 26,773
  • 63
  • 143
  • 176

5 Answers5

2

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

How to check if a String is numeric 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(); 
} 
Community
  • 1
  • 1
Dead Programmer
  • 12,427
  • 23
  • 80
  • 112
  • How will you implement the `isNumeric` method? – dogbane Sep 28 '10 at 09:58
  • 1
    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 Sep 28 '10 at 10:19
1
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.

jmj
  • 237,923
  • 42
  • 401
  • 438
0

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

foret
  • 728
  • 6
  • 14
  • 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 Sep 28 '10 at 10:37
0

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
  • 13,577
  • 18
  • 69
  • 108
0

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
  • 2,445
  • 4
  • 36
  • 56