2010-09-28 118 views
2

嗨当​​我运行下面的代码我得到NumberFormatException任何人都可以帮助我在调试代码。这段代码有什么问题?

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

}} 
+2

首先,请格式化您的代码。其次,异常发生在哪条线上? – Bobby 2010-09-28 09:28:19

+0

如果代码不能编译,IDE(Eclipse,Netbeans,IntelliJ IDEA等)应该解释代码中出现哪些错误以及原因。 – 2010-09-28 09:34:09

+0

检查此链接http://forums.sun.com/thread.jspa?forumID=54&threadID=762831 – Emil 2010-09-28 10:08:09

回答

0

也许因为readline字符串就像'123 \ n'。

+0

不,因为readLine()返回没有行终止字符串的行:http://download.oracle.com/ javase/6/docs/api/java/io/BufferedReader.html#readLine%28 – Mnementh 2010-09-28 10:37:18

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

问题就在这里。

您正在阅读一些非数字输入并尝试将其解析为int。这是例外情况。

2

这个问题我在我今天的工作中遇到过。该bf.readLine()给你空字符串("")或字符值[A-Z]。所以做一个先决条件检查像

// 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. 
     ) 

更新:

// isNumeric implementation Due credit to CraigTP 

请到辉煌逻辑

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

您将如何实现'isNumeric'方法? – dogbane 2010-09-28 09:58:24

+1

这种'isNumeric'方法在这种情况下不起作用,因为它也允许双打。例如,字符串“5.5”将通过'isNumeric'检查,但在'Integer.parseInt'上抛出'NumberFormatException'。 – dogbane 2010-09-28 10:19:57

0

我替换扫描仪的输入流。

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


    } 


}} 
0

对于类型相互兼容的数据类型,我们只能使用显式类型转换。但是当你试图做

ch=(char)bf.read(); 

你实际上是想投一个整数为char类型转换(因为bf.read的返回类型()为int)。但是Integer和char不兼容,所以错误。