2

Edit

Many users are commenting that the Class Word is useless, which is probably true in this case. The reason I added it, is because I need it later on in the program.

This program has 3 classes - WordList, Word and a test class. I'm trying to get the method 'readBook' to read through a file, and send every word over to the method 'addWord'. Method addWord will check if the ArrayList allWords contains that word. If it doesn't, addWord will then add the word to an array, aswell as to send it over to class Word. When I run the program, nothing happens. I tried to print out allWords.size(), which returned 0.

Class WordList:

public class WordList {

String nextWord;
ArrayList<String> allWords = new ArrayList<String>();

public void readBook (String filename) throws Exception{
    File file = new File(filename); //File has one word on each line.
    Scanner innFile = new Scanner(file);
    for (int i = 0; i<file.length(); i++){
        if(innFile.hasNextLine()){
            nextWord = innFile.nextLine();
            addWord(nextWord);
        }
    }
}
private void addWord(String word){
    for (String check : allWords){
        if (!check.equalsIgnoreCase(word)){
            allWords.add(word);
            new Word(word);
        }
        else if(check.equalsIgnoreCase(word)){
            System.out.println("The word allready exsist.");
        }
        else{
            System.out.println("Something went wrong.");
        }
    }
}

Class Word:

public class Word {

String word;
ArrayList<String> allWords = new ArrayList<String>();

Word(String text){
    word = text;
    allWords.add(word);
    System.out.print(allWords);

}

The test class:

public class TestClass {
public static void main (String[] args) throws Exception{
    WordList list = new WordList();
    list.readBook("path.../scarlet.text");

    WordList newList = new WordList();
    System.out.println(newList.numberOfWords());//A method printing out allWords.size()

    }
}
3
  • 1
    You probably do not want to use the ArrayList class or any List in general - this is a classical problem that can easily be solved with a Set. In particular, you can use the HashSet or LinkedHashSet implementations. Commented Oct 13, 2015 at 18:28
  • Also, why does each word contain a wordList on it's own? This doesnt seem like it makes much sense. Commented Oct 13, 2015 at 18:29
  • 1
    If you don't want allWords to contain duplicates, declare it as Set<String> (or TreeSet<String> if you want to have the words in alphabetical order). Commented Oct 13, 2015 at 18:30

6 Answers 6

3

You are populating allWords list of WordList class inside for (String check : allWords). Initially it would be empty hence it will never enter the for loop and allWords will never get populated. In turn new Word(word) will not be called and allWords of word class will be empty.

Sign up to request clarification or add additional context in comments.

3 Comments

"In turn new Word(word) will not be called and allWords of word class will be empty." To be honest: he shouldn't care. allWords is a normal field and since he is throwing each Word instance away, he can't use that list anyway.
@Tom Functionally it doesn't make a difference, but it does complicate the code to readers more than necessary.
@NikG The whole Word makes no sense and is badly designed, so it should be completely removed.
1

You have two issues with your code.

First, when that main loop (for (String check : allWords)) runs, allWords is going to be empty. Therefore, you will never add any elements to it, and that means it will always have a size of 0. To correct this, you probably need to add a boolean variable that gets set to true if you find the word. Then, after the loop, if the boolean variable is still false, add the word to the list.

Secondly, you've got allWords defined on two places: in your WordList class, and in your Word class. The WordList.allWords array is being updated correctly (as far as I can tell, once you fix the above mentioned issue). However, the Word.allWords array is not doing anything other than storing a single String value... twice (once in the array, once in a variable). The Word class isn't really doing anything useful, so I would opt to get rid of it.

I would get rid of the Word class completely, since it's currently not doing anything other than storing a String, which you could do with a String variable.

1 Comment

Also he's making a newList object which doesn't make any sense because the entire list of words will be included in list object in his main method.
1

When the method addWord(String) is called it never enters the for loop because allWords is initially an empty ArrayList. Your call to "new Word(String)" is never reached.

Comments

0

I don't think you need allWords in both the Word class and the WordList class (?)

If you're just trying to get the unique words you can do this:

    Set<String> words = new LinkedHashSet<>();
    File file = new File("some file"); 
    Scanner inFile = new Scanner(file);
    for (int i = 0; i < file.length(); i++) 
        if (inFile.hasNextLine()) 
            words.add(inFile.nextLine());
    inFile.close();

then call

    words.size()

Comments

-1

to check if an array list contains a certain string you can use a for loop. I'm not sure if this is the best way to go about it, but it should work.

for(int i = 0; i<yourArrayList.size(); i++){

if (yourArrayList.get(i).!contains(yourString)){
yourArrayList.add(yourString);
}

Comments

-1

In test class try:

public static void main(String[] agrs) throws Exception {
    WordList w = new WordList();
    w.readBook("pathToMyFile"); // This way you access to readBook method
    ....
}

And add the word in method addWord when attribute allWords is empty.

private void addWord(String word){
    if (allWords.isEmpty()) {
       allWords.add(word);
    } else {
       // Your code
    }
}

1 Comment

How is this supposed to help? Also, because readBook is declared to throw Exception, you have to handle that exception. The original did by having main able to throw an Exception, but yours doesn't, so I don't think will even compile.

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.