0

How do I create a method that takes a string as an argument, and returns the array whose elements are the words in the string.

This is what I have came up with so far:

// split takes some string as the argument, and returns the array
// whose elements are the words in the string
public static String[] split (String s)
{
    // determine the number of words
    java.util.Scanner t = new java.util.Scanner (s);
    int countWords = 0;
    String w;
    while (t.hasNext ())
    {
        w = t.next ();
        countWords++;
    }
    // create appropriate array and store the string’s words in it
    // code here
}

As you can see, I can just input each word via Scanner. Now I just have to put all the words of the String into an array as the elements. However, I'm not sure how to proceed.

1
  • 6
    Use the ready split method: str.split("\\s+");. Commented Jan 2, 2015 at 16:16

3 Answers 3

4

You can use StringTokenizer in java to devide your string into words:

StringTokenizer st = new StringTokenizer(str, " ");

And your output st whould be an array of words.

Take a look at this Java StringTokenizer tutorial for further information.

Your code whould look like:

    StringTokenizer st = new StringTokenizer(s, " ");
    int n=st.countTokens();
    for(int i=0;i<n;i++) {
       words[i]=st.nextToken();// words is your array of words
    }
Sign up to request clarification or add additional context in comments.

2 Comments

Note the javadoc actually recommends using split over this legacy class :)
Thanks @Reimeus: I will keep it in mind. And Split() seems to be faster too referring to this.
1

As Maroun Maroun commented, you should use the split(regex) method from Strings, but if you want to do this by yourself:

First, declare the array:

String[] words = new String[50]; // Since you are using an array you have to declare
                                 // a fixed length.
                                 // To avoid this, you can use an ArrayList 
                                 // (dynamic array) instead.

Then, you can fill the array inside the while loop:

while (t.hasNext()) {
    w = t.next();
    words[countWords] = w;
    countWords++;
}

And finally return it:

return words;

Note:

The sentences

words[countWords] = w;
countWords++;

can be simplified in

words[countWords++] = w;

Comments

0

As @Maroun Maroun said: use the split function or like @chsdk said use StringTokenizer. If you want to use scanner:

public static String[] split(String s)
{
    Scanner sc = new Scanner(s);
    ArrayList<String> l = new ArrayList<String>();

    while(sc.hasNext())
    {
        l.add(sc.next());
    }

    String[] returnValue = new String[l.size()];
    for(int i = 0; i < returnValue.length; ++i)
    {
        returnValue[i] = l.get(i);
    }

    return returnValue;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.