0

Would appreciate if you would help in this code...

I am trying to take a string that has multiple words and store it in an ArrayList and i ended up only storing the first word in the set of words inputted Here is the code: https://code.sololearn.com/cNlryO59q1pg/?ref=app

7th Mar 2020, 9:24 PM
Ishaq Za'rour
5 Respuestas
+ 2
Your code doesn't save only the last word from multiword string. When the for-loop finishes variable "temp" may contain some word (if the last character of the string is not space), but you don't store its value. Append an extra space at the end of the multiword string so that variable "temp" is stored to array on the last iteration of for-loop. You can use String.split(" ") method to split multiword string into array of strings with single words.
7th Mar 2020, 10:20 PM
andriy kan
andriy kan - avatar
+ 3
You should instead take a look at: String[] wordargs = words.split(" "); This will return an array containing strings split by whatever you chose, in this case a space.
7th Mar 2020, 10:17 PM
coddy
coddy - avatar
+ 2
for(String x: names.split(" ")){ namesArr.add(x); }
7th Mar 2020, 11:00 PM
rodwynnejones
rodwynnejones - avatar
+ 1
or if(names.charAt(x)==' ' || x==names.length()-1) {
8th Mar 2020, 4:14 AM
zemiak
0
thank you for your help
8th Mar 2020, 7:27 AM
Ishaq Za'rour