java - Error reading multiple lines of integers using BufferedReader -


i getting exception:

exception in thread "main" java.lang.numberformatexception: input string: "55 45 65 88 "     @ java.lang.numberformatexception.forinputstring(unknown source)     @ java.lang.integer.parseint(unknown source)     @ java.lang.integer.valueof(unknown source) 

while using code:

bufferedreader br = new bufferedreader(new inputstreamreader(system.in));  string line = br.readline(); int noofstudents = integer.parseint(line); //firstline contains 1 integer.  arraylist <integer> marks = new arraylist<integer>();  line = br.readline(); //second line contains line of integers  stringtokenizer tokens = new stringtokenizer(line, "\\s+"); // break line tokens   while (tokens.hasmoretokens()) {     marks.add(integer.valueof(tokens.nexttoken())); //error here     //when converting tokens integers giving error } 

sample input:

4
55 45 65 88 (here, when press enter gives me above stated errors)

stringtokenizer doesn't support regex.

stringtokenizer tokens = new stringtokenizer(line, "\\s+"); // literal "\s+" string token. 

use instead,

stringtokenizer tokens = new stringtokenizer(line, " "); // space. 

edit: @masterodin has pointed out, stringtokenizer's default delimiter space " ". hence below work same way,

stringtokenizer tokens = new stringtokenizer(line); 

Comments

Popular posts from this blog

android - Gradle sync Error:Configuration with name 'default' not found -

java - Andrioid studio start fail: Fatal error initializing 'null' -

html - jQuery UI Sortable - Remove placeholder after item is dropped -