John123 John123 - 26 days ago 9
Java Question

How to create an array list of strings using the .split() function?

I'm attempting to create a program which must abide by the following:


  1. Create a class called
    WordGroup
    which;

  2. Has an instance variable called
    words
    .

  3. Has a constructor which takes a String. This is converted into lower case and stored in words. Use a method from the String API to make the string lower case.

  4. Has a method called
    getWordArray()
    which returns a
    String[]
    . Use the String class
    split()
    method to separate words on " ".

  5. Create two WordGroups one initialized with "You can discover more about a person in an hour of play than in a year of conversation" and the other with "When you play play hard when you work dont play at all" //these are quotes by Plato and Roosevelt respectively with the punctuation removed

  6. Use
    getWordArray()
    to make two arrays of Strings.

  7. Write two for loops to loop over the two arrays and print out each word.



I'm currently stuck on number 6. I have two WordGroups created in my main method but I am unsure of how I would assign them to the
getWordArray()
method so that they would create a string of arrays. Here is the code:

WordGroup class

public class WordGroup {

String word;

//Creates constructor which stores a string value in variable "word" and converts this into lower case using the lower case method.
public WordGroup(String aString) {
this.word = aString.toLowerCase();
}
public String getWordArray; {

word =("");
String WordArray[] = word.split("-");

}
}


Main class

public class Main{

public static void main (String[] args) {

WordGroup firstWordGroup = new WordGroup.word("You-can-discover-more-about-a-person-in-an-hour-of-plau-tban-in-a-year-of-conversation");
WordGroup secondWordGroup = new WordGroup ("When-you-play-play-hard-when-you-work-dont-play-at-all");

}
}


So to be clear, I want to create two array lists that use the
.split()
function to make an array of strings using the
getWordArray()
method and then print the list of arrays out. Any help would be much appreciated, thanks.

Answer

Your code does not compile. The right version of getWordArray is as follows:

public String[] getWordArray() {
    String[] wordArray = word.split("-");
    return wordArray;
}

If you don't return the assigned variable from method, Main class will never know about it. And corresponding main class would be:

public class Main{
    public static void main (String[] args) {
        WordGroup firstWordGroup = new WordGroup.word("You-can-discover-more-about-a-person-in-an-hour-of-plau-tban-in-a-year-of-conversation");
        WordGroup secondWordGroup = new WordGroup ("When-you-play-play-hard-when-you-work-dont-play-at-all");

        String[] firstWordArray =  firstWordGroup.getWordArray();
        for( String word : firstWordArray) { 
            System.out.println(word);
        }
        //second loop is very similar.
    }   
}
Comments