2014-09-19 117 views
0

输入:得到错误,而读整数输入

2 

我的程序:

public static void main(String args[]) throws Exception { 
     /* 
     * Read input from stdin and provide input before running*/ 

     BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 


     String line = br.readLine(); 

     int cases = Integer.parseInt(line);} 

错误:

Exception in thread "main" java.lang.NumberFormatException: For input string: "2 " 
    at java.lang.NumberFormatException.forInputString(Unknown Source) 
    at java.lang.Integer.parseInt(Unknown Source) 
    at java.lang.Integer.parseInt(Unknown Source) 

如何解决这个我想,案件的值= 2 ;什么是错的

+1

http://stackoverflow.com/questions/2506077/how-to-read-integer-value-from-the-standard-input-in- java的 – kosa 2014-09-19 17:50:23

回答

3

您需要trim您的字符串

int cases = Integer.parseInt(line.trim());}