jeudi 23 juillet 2015

I am trying to use an if/else statement incorporating the java scanner for a username and password application

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!");
    }
}

}

Aucun commentaire:

Enregistrer un commentaire