I'm attempting to create a program which must abide by the following:
- Create a class called
WordGroup
which; - Has an instance variable called
words
. - 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.
- Has a method called
getWordArray()
which returns aString[]
. Use the String classsplit()
method to separate words on " ". - 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
- Use
getWordArray()
to make two arrays of Strings. - 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.