0

hello friends,Could you tell me why my array after split method contains just one element?

https://code.sololearn.com/c56z8HO0wWAp/?ref=app

18th Feb 2021, 11:45 AM
Theo Martier
Theo Martier - avatar
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();
18th Feb 2021, 11:58 AM
Jayakrishna 🇮🇳
+ 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 ;)
18th Feb 2021, 12:01 PM
visph
visph - avatar
+ 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
18th Feb 2021, 12:00 PM
你知道規則,我也是
你知道規則,我也是 - avatar
18th Feb 2021, 12:04 PM
Matthew
Matthew - avatar