-1

I am trying to Find out how often the line "character" is in a .txt file I tried This:

String line;
int characterAmount;
characterAmountReader = new LineNumberReader(new FileReader(filename));
while ((line = characterAmountReader.readLine()) != null) {
    if(line == "<character>") {
        characterAmount++;
    }
}

but for some reason it doesn't work : /

Leres75
  • 1
  • 1

1 Answers1

1

Your problem is that you are comparing String using == and not the method equals()

See the full explanation here

To be simple == compare instances. Since line and "<character>" are not the same instance of the class String, it always return false. Using the equals() method compare the value, in this case it should return true when the line value is "<character>".

vincrichaud
  • 2,218
  • 17
  • 34