+ 1

Any one help me please, Why I can't able to store the String, into the array of String by using split function.

https://code.sololearn.com/ck6H6iAW39Wr/?ref=app I can able to store the String in, array of String only if I declare the 2 other variables (NoOfAgents and name) into comments Why it's happening I don't know Please tell me if you know

8th Oct 2021, 4:20 AM
No Mercy
No Mercy - avatar
2 odpowiedzi
+ 2
nextInt() reads input for <noOFAgents> and leaves a line break character '\n' in the input stream after reading. When you try to read <nameOfFour> using nextLine(), the leftover line break character is accidentally read into <nameOfFour>, instead of what was supposed to be read in (4 names separated by space). To overcome this, you need to consume the leftover line break character from the input stream by issuing a call to myVar.nextLine() after reading <noOFAgents>. int noOFAgents= myVar.nextInt(); myVar.nextLine(); // <-- consume leftover line break String nameOfFour = myVar.nextLine(); String sepNames[] = nameOfFour.split(" ");
8th Oct 2021, 6:21 AM
Ipang
+ 1
Don’t use scanner.nextInt try it with this input and see why: name 2 name name
8th Oct 2021, 4:37 AM
Kamen Studentov
Kamen Studentov - avatar