开发者

What's the problem with this code?

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

}}


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


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.


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


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


   }


}}


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜