Charles
Charles

Reputation: 57

System.out.println() is being skipped

My prompt "Please Enter your ID" just after the second for loop is not appearing, it goes straight to "Please enter your password." It also skips alot of code from the Login Prompt to the Password prompt. If you have any ideas as to why it behaves like this please share it with me thanks.

public void main(String[] args){

    Accounts Accounts = new Accounts();

    Scanner kb = new Scanner(System.in);

    System.out.print("Please create a login ID and hit \"ENTER\": ");
    Login = kb.nextLine();

    for(;;){
        if(!Accounts.isTaken(Login)){
            System.out.print("Please create a password and hit \"ENTER\": ");
            PW = kb.nextLine();
            if(Accounts.validPW(PW)){
                Accounts.createAccount(Login, PW);
                break;
            }
        }
    }

    System.out.print("Do you wish to Log in ? (Y/N): ");
    String response = kb.nextLine();
    if((response=="y")||(response=="Y")){
        for(;;){
           //Not Being Shown
            System.out.println("Please enter your ID: "); // ?????? Where are you?????
            Login = kb.nextLine();
            Accounts.login(Login);
            //Goes straight here
            System.out.print("Please enter your Password: ");
            if ((Accounts.isValid(Login))&&(Accounts.checkAuth(kb.nextLine()))){
                break;
            }
            else{
                System.out.println("Invalid Login-Password!");
            }
        }
    }
    System.out.println("Please enter your Password: ");
    System.out.println("LOGIN AUTHORIZED: "+Accounts.checkAuth(kb.nextLine()));
 }

Upvotes: 4

Views: 290

Answers (3)

kosa
kosa

Reputation: 66637

Instead of

(response=="y")||(response=="Y")

use

(response.equals("y"))||(response.equals("Y"))

Upvotes: 4

PermGenError
PermGenError

Reputation: 46408

you are using == operator to check string equality. use equals() method.

== operator:

  1. For primitive variables checks if two primitives have same value.
  2. For objects: checks if two reference variables point(refer) to the same object.

equals() method

  1. Checks if two objects are meaningfully equal.

    if((response=="y")||(response=="Y")){

should be

if((response.equals("y"))||(response.equals("Y"))){

or even better

if((response.equalsIgnoreCase("y"))){

Upvotes: 3

huseyin tugrul buyukisik
huseyin tugrul buyukisik

Reputation: 11910

Since java is reference-based, response has a differend id than "y". You should use

 response.equals("y")

Upvotes: 0

Related Questions