简体   繁体   中英

Check through an ArrayList if it contains a String. If it doesn't, add that String

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()

    }
}

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.

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.

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.

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()

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
    }
}

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);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM