+ 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
2 RĂ©ponses
+ 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(" ");
+ 1
Donât use scanner.nextInt
try it with this input and see why:
name
2 name name