0
hello friends,Could you tell me why my array after split method contains just one element?
4 Réponses
+ 2
Aftet reading input of int type, reading pointer stays within same line but input is in next line so it just read a space only..
So you sholud add nextLine() after int reading to goes to next line for reading next input from next line..
int agents=data.nextInt();
data.nextLine();
String others= data.nextLine();
+ 2
nextInt consume only digits, so next call to nextLine return empty string (if new line next to int)...
you should make a call to nextLine() before assigning nextLine() return value ;)
+ 1
Assume you input the following:
John
3
Carrie James Tom
name will be John because of data.nextLine() and John is the first line.
agents will be 3 because of data.nextInt() and agents is the first int after the first line.
Here, others will be ' ', a blank, technically a newline '\n'.
Because data.nextInt() only takes the '3' part, the newline after the 3 is not taken. After data.nextInt() it still STAYs at the second line.
Thus, data.nextLine() will immediate take the rest of the second line, which is only '\n'.
If you do add another data.nextLine(), it will returns Carrie James Tom
+ 1
Please delete one of the 2 duplicated questions:
https://www.sololearn.com/Discuss/2700369/?ref=app
https://www.sololearn.com/Discuss/2700362/?ref=app