0

The idea is simple:

  1. Load a text file into a string.
  2. Split this string into paragraphs.
  3. Split all paragraphs into words.
  4. Add each individual word to an ArrayList.

The result is an ArrayList containing all of the words in the text file.

The procedure works; it loads all of the words in an ArrayList fine.

HOWEVER, any "IF" statements to find a specific item in the ArrayList do not work.

EXCEPT: if the word is a newline.

public String loadText(String resourceName){


    // Load the contents of a text file into a string
    String text = "";
    InputStream stream = FileIO.class.getResourceAsStream(resourceName);
    BufferedReader reader = new BufferedReader(new InputStreamReader(stream));
    String str = "";
    try {
        while ((str = reader.readLine())!=null){
            text += str + "\n";
        }
    } catch (Exception e) {
        System.out.println("Unable to load text stream!");
    }
    return text;
}

public void test(){

    ArrayList<String> collectionOfWords = new ArrayList<String>();
    String text = loadText("/assets/text/intro.txt");

    // Split into paragraphs
    String paragraphs[] = text.split("\n");
    for (String paragraph: paragraphs){

        // Split into words
        String words[] = paragraph.split(" ");

        // Add each word to the collection
        for (String word: words){
            collectionOfWords.add(word);
        }

        // Add a new line to separate the paragraphs
        collectionOfWords.add("\n");
    }

    // Test the procedure using a manual iterator
    for (int i=0; i<collectionOfWords.size(); i++){


        // ===== WHY DOES THIS WORK?
        if (collectionOfWords.get(i)=="\n")
            System.out.println("Found it!");

        // ===== BUT THIS DOESN'T WORK???
        if (collectionOfWords.get(i)=="test")
                System.out.println("Found it!");

        // NOTE: Same problem if a I use:
        // for (String word: collectionOfWords){
        //   if (word=="test")
        //     System.out.println("Found it!");
    }
}

Sample of text file: The Quick Brown\n Fox Jumps Over\n test the lazy dog\n

Any ideas? I am at the point to just scratch my design and try something totally different...

Marc Vee
  • 11
  • 2

1 Answers1

2

Short answer: Compare strings using .equals and not ==.

Long answer:

    // ===== WHY DOES THIS WORK?
    if (collectionOfWords.get(i)=="\n")
        System.out.println("Found it!");

This works because you have two "\n" in your program. (One in your .add("\n") and one where you do == "\n". Since these two strings are literals in your program, they will refer to the same object, i.e. a reference equality check (==) will work fine.

    // ===== BUT THIS DOESN'T WORK???
    if (collectionOfWords.get(i)=="test")
            System.out.println("Found it!");

The words you're looking for in your text file doesn't exist in your program code. (They're not literals.) Which means that a string "test" loaded from the file and a the string literal "test" in your program are two different objects (though they contain the same value). To test if two different strings contain the same value, you compare them with equals:

    if (collectionOfWords.get(i).equals("test"))
            System.out.println("Found it!");
aioobe
  • 413,195
  • 112
  • 811
  • 826
  • actually it's even better to use `"test".equals(collectionOfWords.get(i))` – EpicPandaForce Aug 14 '14 at 21:11
  • OMG! Thank you thank you thank you! Is there a short answer why "==" would work for newline, but not for others? No big deal, I can always look it up later. Cheers. – Marc Vee Aug 14 '14 at 21:20