A community for students.

Here's the question you clicked on:

55 members online
  • 0 replying
  • 0 viewing

anonymous

  • 4 years ago

[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++; }

  • This Question is Closed
  1. shadowfiend
    • 4 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    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.

  2. anonymous
    • 4 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    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..

  3. anonymous
    • 4 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    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] ...

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

    • Attachments:

Ask your own question

Sign Up
Find more explanations on OpenStudy
Privacy Policy

Your question is ready. Sign up for free to start getting answers.

spraguer (Moderator)
5 → View Detailed Profile

is replying to Can someone tell me what button the professor is hitting...

23

  • Teamwork 19 Teammate
  • Problem Solving 19 Hero
  • You have blocked this person.
  • ✔ You're a fan Checking fan status...

Thanks for being so helpful in mathematics. If you are getting quality help, make sure you spread the word about OpenStudy.

This is the testimonial you wrote.
You haven't written a testimonial for Owlfred.