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
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.
+ 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.
+ 2
for(String x: names.split(" ")){
namesArr.add(x);
}
+ 1
or
if(names.charAt(x)==' ' || x==names.length()-1) {
0
thank you for your help