0

I'm trying to get info from a file (it's to be used as a settings file, true or false values), using the following code:

File information = new File(aPath + "/plugins/ArenaCraft/info.acs");

ArrayList<String> info = new ArrayList<String>();
BufferedReader br = new BufferedReader(new FileReader(information));
String line;
while((line = br.readLine()) != null) {
     String[] sLine = line.split(":");
     if(sLine[1] == "true"){
         info.add("true");
     }
     if(sLine[1] == "false"){
         info.add("false");
     }
     if(sLine[1] == null){
         System.err.println("[ArenaCraft]  sLine[1] is NULL.");
     }
     if(sLine[1] != "true" && sLine[1] != "false" && sLine[1] != null){
         System.err.println("[ArenaCraft]  sLine[1] is not NULL, but it's not \"true\" or \"false\".");
         System.err.println("[ArenaCraft]  sLine[1] is: " + sLine[1]);
     }
}

br.close();
return info;

This, however, is outputting the

[ArenaCraft]  sLine[1] is not NULL, but it's not "true" or "false".

message. It then outputs, exactly,

[ArenaCraft]  sLine[1] is: true

So, I don't understand why it says it's not null or true or false. Do you guys imagine why that would happen?

The contents of the file I'm trying to read, with no encoding, follow:

notices:true

and nothing more.

Any help would be greatly appreciated, thanks in advance.

HolyThunder
  • 463
  • 1
  • 6
  • 17
  • 2
    Don't compare String values using `==`. Use `equals()` – Alexis C. Dec 22 '13 at 18:40
  • 3
    Congratulations! You are the winner of the "Who posts the == vs. equals question on Dec 22th" contest. – Ingo Dec 22 '13 at 18:42
  • Sorry, but I didn't know what was causing the problem so I didn't check for exactly comparing strings. I'm sort of new to java, wasn't sure where the problem could lay in. – HolyThunder Dec 22 '13 at 19:26
  • 2
    @HolyThunder Don't worry. It's just a bit sarcasm because problems rooted in the same problem as your question come up every single day at least once. So one could say it's a weakness of Java. – Ingo Dec 22 '13 at 22:47

4 Answers4

1

You are using a==b instead of a.equals(b) See: Java String.equals versus ==.

Community
  • 1
  • 1
k_g
  • 4,333
  • 2
  • 25
  • 40
1

Well, the == operator compares references. You want to use the equals() method here:

if (sLine[1].equals("true")) {
    [...]
}

Also, you should use the String.trim() method, to ensure that there are no leading or trailing spaces in your strings. A line like this: param = true would parse to " true" with your code, which is not equal to "true".

if (sLine[1].trim().equals("true")) {
    [...]
}
Sibbo
  • 3,796
  • 2
  • 23
  • 41
1

In general, change:

if (cond1) { ... }
if (cond2) { ... }  
if (cond3) { ... }
if (!cond1 && !cond2 && !cond3) { .... }

to

if (cond1) { ... }
else if (cond2) { ... }
else if (cond3) { ... }
else { ... }

in cases when at most one of the conditions can be true, like in your example.

Ingo
  • 36,037
  • 5
  • 53
  • 100
  • Thanks! I'm rather new to java, I know some and can work my way around most basic stuff but I don't know these kind of guidelines! – HolyThunder Dec 22 '13 at 19:23
0

Use equals() method for String content comparison , == for String reference comparison

Prabhaker A
  • 8,317
  • 1
  • 18
  • 24