anonymous
  • 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++; }
Computer Science
  • Stacey Warren - Expert brainly.com
Hey! We 've verified this expert answer for you, click below to unlock the details :)
SOLVED
At vero eos et accusamus et iusto odio dignissimos ducimus qui blanditiis praesentium voluptatum deleniti atque corrupti quos dolores et quas molestias excepturi sint occaecati cupiditate non provident, similique sunt in culpa qui officia deserunt mollitia animi, id est laborum et dolorum fuga. Et harum quidem rerum facilis est et expedita distinctio. Nam libero tempore, cum soluta nobis est eligendi optio cumque nihil impedit quo minus id quod maxime placeat facere possimus, omnis voluptas assumenda est, omnis dolor repellendus. Itaque earum rerum hic tenetur a sapiente delectus, ut aut reiciendis voluptatibus maiores alias consequatur aut perferendis doloribus asperiores repellat.
jamiebookeater
  • jamiebookeater
I got my questions answered at brainly.com in under 10 minutes. Go to brainly.com now for free help!
shadowfiend
  • 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.
anonymous
  • 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..
anonymous
  • 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] ...

Looking for something else?

Not the answer you are looking for? Search for more explanations.