0

I am trying to use apache commons to read a dictionary. When I use this code

for(String line: FileUtils.readLines(new File("dictionary.txt")))
    System.out.println(line);
}

it prints the whole file. But when i use this code nothing happens when i am 100% sure b and line are the same

    for(String line: FileUtils.readLines(new File("dictionary.txt")))
    {
        if(line.toLowerCase() == b.toLowerCase())
            {
                 valid = true;
             System.out.println(line);
        }

So whats wrong?

EJK
  • 12,332
  • 3
  • 38
  • 55
Welsar55
  • 39
  • 6

1 Answers1

2

In Java, with the exception of the primitive types, "==" tests whether two objects are the same object (i.e. do they share the same memory address) whereas .equals() tests whether they have equivalent values (even if they are stored in different memory locations). Simply change:

  line.toLowerCase() == b.toLowerCase()

to:

  line.equalsIgnoreCase(b)

... and it should work.

Michael Aaron Safyan
  • 93,612
  • 16
  • 138
  • 200