Whitespace split(“ “) not working with input | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Whitespace split(“ “) not working with input

https://code.sololearn.com/cmNpyjohbQkE/?ref=app If you take off the comment dashes from the print statement, you will find that inputting “Grr Grr” does not become two indexes in an array when using .split(“ “) because it will return the length of the array as a single index. If someone could help me split the array by whitespaces properly it would be appreciated, thank you.

8th Jun 2021, 11:06 PM
Cesar Saavedra
2 Answers
+ 4
split works as expected... your mistake is you are taking user input though next() method, wich return input until space like character encountered, so even if you enter "Grr Grr", the 'noise' string variable only contains the first "Grr"... you must fix it by using nextLine() method, wich return user input until next new line char encoutered: String noise = input.nextLine();
8th Jun 2021, 11:53 PM
visph
visph - avatar
+ 1
Thank you, it worked perfectly in my program
9th Jun 2021, 12:11 AM
Cesar Saavedra