java - Getting issues while reading series of lines using scanner -


i'm trying read input in following format.

2 asdf asdf 3 asd df 2 

following code:

scanner scanner = new scanner(system.in); int t = scanner.nextint(); system.out.println(t); while(t>0){      string = scanner.next();     string b = scanner.next();     int k = scanner.nextint(); }        

but when i'm scanning, i'm getting empty t=2 , a="" , b=asdf, k=asdf

can't figure out issue. there no space/new line between 2 , asdf.

i have tried using scanner.nextline() instead of scanner.next() no change

nextint() doesn't cosume newline token, following read it. introduce nextline after nextint skip it:

scanner scanner = new scanner(system.in); int t = scanner.nextint(); scanner.nextline(); // skip newline character system.out.println(t); while(t > 0) {     string = scanner.next();     string b = scanner.next();     int k = scanner.nextint();     scanner.nextline(); // skip newline character } 

Comments