简体   繁体   中英

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()));
 }

Instead of

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

use

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

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"))){

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

 response.equals("y")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM