0

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.

mtyurt
  • 3,369
  • 6
  • 38
  • 57
John123
  • 13
  • 5
  • Possible duplicate of [What's the simplest way to print a Java array?](http://stackoverflow.com/questions/409784/whats-the-simplest-way-to-print-a-java-array) – ItamarG3 Nov 06 '16 at 18:15
  • 1
    `public String getWordArray; { ... }` doesn't do what you think it does. That is not exactly what point 4 is telling you to do. – Pshemo Nov 06 '16 at 18:17

2 Answers2

0

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.
    }   
}
mtyurt
  • 3,369
  • 6
  • 38
  • 57
0

Your getWordArray isn't doing what #4 is telling you it should do. You need the square brackets [] after String to tell it to return an array of strings. You also do not have a return statement.

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

How you would use this to create to arrays of strings in #6 is calling your method from the two variables you created.

String[] wordArray1 = firstWordGroup.getWordArray();
String[] wordArray2 = secondWordGroup.getWordArray();
Matthew Wright
  • 1,485
  • 1
  • 14
  • 38