0

So I am asking the user for their username, and if the user types in "username" then he/she will have "Correct, that is your username!" displayed and if they get it wrong they will get "Incorrect, that is not your username!" displayed.

The problem I am having is that whenever I type the username, in this example the username being "username" I have the Incorrect message displayed.

I would like to know where I went wrong and if this question has been asked a million times if someone could direct me to one of those posts where this has been solved that would be great since I can't seem to find one that's like this problem.

import javax.swing.*;

import java.util.Scanner;

public class userID {

public static void main(String[] args) {

    Scanner input = new Scanner(System.in);

    String username;
    String password;
    String email;
    String scanner1;

    username = "username";
    password = "password";
    email = "emailhere";

    System.out.println("------------------------------------------");
    System.out.println("Welcome to the ID memory tester");
    System.out.println("Author: me");
    System.out.println("------------------------------------------");

    System.out.println("What is your username?");


    scanner1 = (input.nextLine());


    if (scanner1 == username) {
        System.out.println("Correct, that is your username!");
    } else {
        System.out.println("Incorrect, that is not your username!");
    }
}

}

KevinDavid
  • 11
  • 4

1 Answers1

3

== operator compares references.

You need to use .equals().

Change it to

   if (scanner1.equals(username)) {
       System.out.println("Correct, that is your username!");
   } else {
       System.out.println("Incorrect, that is not your username!");
   }
Am_I_Helpful
  • 18,735
  • 7
  • 49
  • 73
James Wierzba
  • 16,176
  • 14
  • 79
  • 120
  • About 1 minute after posting something snapped and I remembered something about ".equals()." Thank you very much! – KevinDavid Jul 23 '15 at 21:35
  • @KevinDavid-If your issue gets resolved,please accept this answer. See how to accept the answer ---> http://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work – Am_I_Helpful Jul 23 '15 at 21:36
  • 1
    Was about to but it says I have to wait 7 minutes to do that. I guess something to do with reputation or very early responses! Will do in 7 minutes though. – KevinDavid Jul 23 '15 at 21:38