0

I have the following code to read a line from a file and if it is equal to "true", set a boolean value to true, and if not, set the boolean to false:

BufferedReader reader = new BufferedReader(new FileReader(optionsFile));
stringFromFile = reader.readLine();
System.out.println("SFX plain text: " + stringFromFile);
if (stringFromFile == "true") {
    SFX = true;
}
else {
    SFX = false;
}
System.out.println("SFX: " + SFX);

However, when executed, the following comes up in the log (I have excluded the package name because it has personal details on it):

04-09 23:35:39.980 9686-9686/<PACKAGE NAME> I/System.out: SFX plain text: true
04-09 23:35:39.980 9686-9686/<PACKAGE NAME> I/System.out: SFX: false

Obviously, the if statement is evaluating the string incorrectly, but I've tried many things, and I've been unable to fix it. Does anyone know what's wrong, or how to fix it? Any help would be much appreciated.

P.S: If it's relevant, the text file is written with the following code:

writer = new BufferedWriter(new FileWriter(optionsFile));
if (SFX) {
    writer.write("true");
    System.out.println("SFX: true");
}
else {
    writer.write("false");
    System.out.println("SFX: false");
}
writer.newLine();
writer.close();
Dab1001
  • 3
  • 2
  • Look at String.equals and String.compareTo. String contents are not compared with the == operator only the references. If the strings are internal and the same it will evaluate true but that isn't how String objects are compared. – Andrew T Finnell Apr 09 '16 at 23:51
  • That worked, thanks for clearing it up. I'll keep that in mind in the future. – Dab1001 Apr 10 '16 at 10:24

0 Answers0