0

Input:

2

My program:

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

Error:

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)

How to solve this i want that the value of cases=2; what's wrong

Bad Coder
  • 696
  • 1
  • 8
  • 21
  • 1
    http://stackoverflow.com/questions/2506077/how-to-read-integer-value-from-the-standard-input-in-java – kosa Sep 19 '14 at 17:50

1 Answers1

3

You need to trim your string

int cases = Integer.parseInt(line.trim());}
StackFlowed
  • 6,575
  • 1
  • 26
  • 41