Question

I have a program which can recognize specific words from a file .txt

The problem is when find a word I send it to a method like "value" and I question:

if (value == "specificword") {...}

this question is always false. I have made many debugs and I'm sure both are the same word (without a space or tab or enter) so: Is it possible this be a problem with the text format?

Was it helpful?

Solution

You need to use equals method for string comparision. Change this

if (value == "specificword") {...}

to

if (value.equals("specificword")) {...}

equals method compares the string contents while == checks for object equality. Read this related post for more info:

Java String.equals versus ==

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top