0

Possible Duplicate:
Java String.equals versus ==

I am using jcreator to practice java language. I came up with a conditional statement in which if the user input is = "test" it will print an "okay!" message. This is my code:

class conditional {

    public static void main (String[] args) {

        Scanner user_input = new Scanner(System.in);

        String username;
        System.out.print("username: ");
        username = user_input.next();
            if (username == "test") {
                System.out.println("okay");
                    }
                    else {
                                    System.out.println("not okay");
    }
}

The above code does not show any error, it does not display the "okay" && "not okay" message either. I am not sure what's wrong with my logic.

Community
  • 1
  • 1
user1645034
  • 151
  • 3
  • 4
  • 8

6 Answers6

3

Strings should be compared using .equals rather than ==. This is the case for all non-primitive comparisons. For example, you would compare two int fields with ==, but because Strings are not primitive, .equals is the correct choice.

if (username.equals("test")) {
FThompson
  • 28,352
  • 13
  • 60
  • 93
1

You should use String.equals here.

if (username.equals("test")) {

Otherwise, you're comparing the identities of the objects rather than their semantics. In fact, you have two separate Strings here, which satisfy semantic equality.

obataku
  • 29,212
  • 3
  • 44
  • 57
0

Or If you want to use == do username.intern(); Then you can use ==.

Note: Not a recommended approch just for FYI.

Amit Deshpande
  • 19,001
  • 4
  • 46
  • 72
  • Not `username.intern();` -- that is a statement. You need to use the result of `username.intern()` in your comparison. That all being said, internalized strings are stored in a special part of the heap and are exempt from garbage collection. As a result, if the `String` internally holds a reference to e.g. a larger `char[]` (often the case with `String.substring` results), you can potentially be wasting quite a bit of memory without realizing it. – obataku Sep 05 '12 at 06:06
0

as @veer said, you can use equalsIgnoreCase / equals

if (username.equals("test")) { ... }

Or

You can use compareToIgnoreCase / compareTo

if (username.compareTo("test")==0) { ... }
Chandra Sekhar
  • 16,256
  • 10
  • 67
  • 90
0

To compare string you need to use .equals() method and also if you need ignore the case of the letters in the String you can use .equalsIgnoreCase()

 if (username.equals("test")){

 }
 else{

 }
mssb
  • 878
  • 1
  • 8
  • 19
0

It will work if you do it like this:

import java.util.Scanner;

class Conditional {
    public static void main(String[] args) {
        Scanner user_input = new Scanner(System.in);
        String username;

        System.out.print("username: ");
        username = user_input.next();

        if (username.equals("test")) {
            System.out.println("okay");
        } else {
            System.out.println("not okay");
        }

        user_input.close();
    }
}

A few remarks:

  • Class names should start with an upper case.
  • Use equals to compare Strings.
  • You state that no result is printed at all. You will have to click in the console to type your answer and end the input with Enter.
brimborium
  • 9,362
  • 9
  • 48
  • 76