0

I wrote a simple little program to ask for a user ID, a Username, and a password. So far, I have the user ID working but the Username doesn't work. I think it has something to do with comparing Strings, but alas, I do not know.

Most of the important code is located in login.class. I am using Account objects stored in an array where the user inputs an ID and the account stored at the place equal to the id (for instace Accounts[4] would be an account, Derp, and the user access it by entering in the ID of 4) then inputs a username, and then a password. I do not wish to refine the program further than becoming operational, but advice is always welcome.

public class Login {

static boolean IDisOK = false;
static boolean usernameisOK = false;
static boolean passwordisOK = false;

static Scanner input = new Scanner(System.in);
static Scanner input2 = new Scanner(System.in);

static Account test = new Account("guest", "blank", 0);
static Account[] accounts = new Account[1];

static int tmpID = -1;
static String tmpUsername = "has not been entered, weird";
static String tmpPassword = "has not been entered, weird";
static Account tmp;

public static void initAccounts() {
    accounts[0] = test;
}

public static void getaccID() { // Prompts and asks the user for and ID
    System.out.println("Please enter in your account ID");
    tmpID = input.nextInt();
    if (tmpID < accounts.length) {
        IDisOK = true;
    }//end of if statement

}//end of getaccID

public static void getaccUsername() { // Prompts and asks user for a username if      the ID was OK
    if (IDisOK == true) {
    System.out.println("Please enter the username linked to the ID: " + tmpID);
    tmpUsername = input2.nextLine();
    if (accounts[tmpID].username == tmpUsername) {
        usernameisOK = true;
        }//end of if statement
    } else {
        System.out.println("Please get a valid ID then come back.");
        System.exit(0);//end of if statement

    }
}//end of getaccUsername

public static void getaccPassword() { //Prompts and asks user for a password is the     username was OK
    System.out.println("Please enter the password set for this username: " +     accounts[tmpID].username);
    tmpPassword = input.nextLine();
    if (usernameisOK == true) {
        if (accounts[tmpID].password == tmpPassword) {
            passwordisOK = true;
            Main.setAuth(true);
        }
    }else{
        System.out.println("Please come back when you have a valid     username.");
        System.exit(0);

    }
}//end of getaccPassword

public static void signIn() {
    initAccounts();
    getaccID();
    getaccUsername();
    getaccPassword();



}//end of signIn method

}//end of class



public class Account {

String username = " ";
String password = " ";
int ID;



public Account(String userIn, String passwordIn, int idIn) {
    this.username = userIn;
    this.password = passwordIn;
    this.ID = idIn;
}//end of constructor



public String getUsername() {
    return this.username;
}
public String getPassword() {
    return this.password;
}
public int getID() {
    return this.ID;
}
public void setUsername(String input) {
    username = input;
}
public void setPassword(String input) {
    password = input;
}

}

public class Main {

static boolean hasbeensaved = false;
static boolean auth = false;
public static void main(String[] args) {
    Login.signIn();
    if (getAuth() == true) {





    }

2 Answers 2

1

Here you are comparing if the memory address of the references are the same

 if (accounts[tmpID].username == tmpUsername)

should be

if (accounts[tmpID].username.equals(tmpUsername))

Just as a note, everything in Java is a reference, except the primitive data types. So if you're comparing object values, you must EVER implement the equals method...

Sign up to request clarification or add additional context in comments.

1 Comment

Haha! Thanks so much! It was a huge help.
0

Another classic == and .equals() miss interpretation example

== compares to see if the 2 reference variables are pointing to the same object, where as in here we want the compare if the 2 string objects have same state, hence use .equals() which returns a boolean true if both String objects have same state, else return false.

try this:

public static void getaccUsername() { // Prompts and asks user for a username if          the ID was OK
    if (IDisOK == true) {
    System.out.println("Please enter the username linked to the ID: " + tmpID);
    tmpUsername = input2.nextLine();
    if (accounts[tmpID].username.equals( tmpUsername)) {// use .equals in string state comparision
        usernameisOK = true;
        }//end of if statement
    } else {
        System.out.println("Please get a valid ID then come back.");
        System.exit(0);//end of if statement

    }
}//end of getaccUsername

1 Comment

Thanks for your input, it was quite useful.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.