Ask your own question, for FREE!
Computer Science 31 Online
OpenStudy (anonymous):

[Java] How do I use the .split() method to split the every word in the sentence? public static Word[] stringsToWords(String[] s) { Word[] result = new Word[60]; int size = 0; for (int i = 0; i < s.length; i++) { // Exercise #1 - split the string into words, and add each word result[i] = new Word(s[i]); size++; }

OpenStudy (shadowfiend):

Seems like s should be a single String, not an array, no? Either way, you'll want to do a string.split(" "). This will split string into an array of strings separated by spaces.

OpenStudy (anonymous):

The answer to your question will depend on how your going to separate a word. (I mean how from a "_(space)", coma,period etc). I assume you will separate a word by a space,coma,period. I'm editing Ur code.. public static Word[] stringsToWords(String[] s) { Word[] result = new Word[60]; int size = 0; int wStart=0; int wEnd=0; int wCount=0; char c; for (int i = 0; i < s.length; i++) { // Exercise #1 - split the string into words, and add each word c=s.CharAt(i); if(c==' ' || c==',' || c=='.'){ wEnd=i; result[wCount]=new Word(); result[wCount].setWord(s.split(wStart,wEnd)); // setWord is the method to put the word into // //Word Objet wStart=wEnd+1; wCount++; continue; } //// size++; } From the above code u can get a idea of the algorithm that i have used... this code might have thousand of compile error because i typed it in Openstudy editor. :) Hope this will help u..

OpenStudy (anonymous):

You are actually bringing in an array of Strings to your function. You would need to loop on the s[] array and then run split on each of the inputs. The split function works by choosing a separator character and returning an array of strings. So if you have a sentence: "The quick brown fox ..." Split would break that into : [0] The [1] quick [2] brown [3] fox [4] ...

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!