0

I've been working on a small personal project and mainly polishing the IO aspect of the program.

I'm mainly having trouble with one method, which is responsible of calling two methods that read one line of text document and dissect it to find a certain string.

public boolean noSavedGame(){
    boolean noGameFound = false;
    read();  //method reads the text document that can have multiple lines
    String temp = find();  //method finds the property and returns it in a string

    if(temp == "none"){ 
        System.out.println("Saved game not found");
        noGameFound = true; //a saved game WAS NOT FOUND

    }
    else{
        System.out.println("saved game found");
        noGameFound = false; //a saved game WAS FOUND
        propertyDetail = temp;
    }

    return noGameFound;
}

So, the first line of the text document looks like this:

<pastSavedGame> - none

read() correctly takes that line and find() correctly returns "none" as its supposed to return the property.

But the conditional is not working right. Even though temp equals "none", the if-statement is executed, not the else statement. At first, I thought whitespace was an issue, but does not seem to be a factor.

I apologize for what may be a very simple question, but I am new to Java. Thank you for the help.

1 Answers1

1

To compare strings use equals() instead of ==.

equals() compares the content; == checks whether it is the same instance.

tomse
  • 501
  • 2
  • 7