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.