-6

Solved in comments, '(tempuser.equals(actualuser)' was the correct way.

This is a snippet of my code;

if (tempuser != "a"){
  System.out.println("FAIL");
    logincount=logincount+1;
  }
else {
  System.out.println("Success");
  }

The program is printing out FAIL, even when I print out the scanned variables and everything is "a".

Any ideas?

Can post more code on request

Kieronboz
  • 85
  • 3
  • 14

7 Answers7

1

String comparison using == is not recommended. The correct way to compare Strings are with equals()

if(!tempuser.equals("a")) {
    // rest of code
}
else {
    // rest of code
}
NilsH
  • 13,705
  • 4
  • 41
  • 59
  • 3
    Not only not recommended; simply does not work – Anya Shenanigans Apr 02 '13 at 13:36
  • It'll work in specific cases, but yes, I agree that it's better to think of it as it won't work. – NilsH Apr 02 '13 at 13:38
  • @Petesh sometimes it works, but you need to compare String literals i.e. `"abc"=="abc"` returns `true`, but `new String("abc")=="abc"` returns `false`, since `new` creates new String, not reusing one from string pool so these objects will not be the same. IMHO this is root of all `String==String` misunderstanding. – Pshemo Apr 02 '13 at 13:41
1

You need to use equals() with an object.

if (tempuser == null || !tempuser.equals("a")) {
  System.out.println("FAIL");
} //etc.

The reason is that == checks to see if it is the same reference. This works for primitives, like int, but for objects you need to rely on the implementation of the equals() method for class specific instructions for testing equivalency.

B. Smith
  • 11
  • 1
0

To compare strings you need to use String.equals

if (tempuser.equals("a"){
msam
  • 4,259
  • 3
  • 19
  • 32
0

Allways use String.equals(otherString) to compare the content of Strings. with == you compare the Object IDs.

Simulant
  • 19,190
  • 8
  • 63
  • 98
0

To compare Strings for equality, don't use ==. The == operator checks to see if two objects are exactly the same object. Two strings may be different objects, but have the same value (have exactly the same characters in them). Use the .equals() method to compare strings for equality. Similarly, use the .compareTo() method to test for unequal comparisons. For example:

String s = "something", t = "maybe something else";
if (s == t)      // Legal, but usually WRONG.
if (s.equals(t)) // RIGHT
if (s > t)    // ILLEGAL
if (s.compareTo(t) > 0) // CORRECT>
TheEwook
  • 11,037
  • 6
  • 36
  • 55
0

Using == and != in Java will check if the references are pointing at the exact same object. This is highly unlikely.

You would want to use the .equals() method instead to check if the content of the Strings are equal.

if (!tempuser.equals("a")){
    // Do stuff
}
ajp15243
  • 7,704
  • 1
  • 32
  • 38
0

As no doubt countless others have said by the time I've submitted this answer, == checks whether the reference to the object on the left (String tempuser in this case) is equal to the reference of the object on the right (String "a"). What you probably want to do is use the String.equals() method, as follows:

if(!tempuser.equals("a")) {
    //Foo
} else {
    //Barr
}

A more comprehensive answer can be seen here.

Community
  • 1
  • 1
Edd
  • 3,724
  • 3
  • 26
  • 33