Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Why does my username enter is false/wrong? Java

this is my code :

import java.util.Scanner;

public class OnlineBookStore {

public static void main(String[] args) {
    
    Scanner in = new Scanner(System.in);
    
    System.out.println("Welcome to my online book store!");
    System.out.println(" ");
    System.out.println("Which user are you?");
    System.out.println("1. Admin");
    System.out.println("2. Buyer");
    System.out.println("3. Seller");
    int option = in.nextInt();
    
    if (option == 1) { 
        
        String user, pass;
        
        System.out.println("Enter your username: ");
        user = in.nextLine();
        
        System.out.println("Enter the password: ");
        pass = in.nextLine();
        
        if(user.equals("Admin1") || (pass.equals("Best"))) {
            System.out.println("test");
        } else { 
            System.out.println("Wrong password.");
        }
    }
}

}

this is the output:

Welcome to my online book store!

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Which user are you?

  1. Admin
  2. Buyer
  3. Seller

1

Enter your username:

Enter the password:

Admin1

Wrong password.

>Solution :

What is happening is that you Scanner only read the int but did not clear the rest of the buffer, what you can do is simply make a call to nextLine() and this will fix your problem!

import java.util.Scanner;

public class Main
{
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
    
    System.out.println("Welcome to my online book store!");
    System.out.println(" ");
    System.out.println("Which user are you?");
    System.out.println("1. Admin");
    System.out.println("2. Buyer");
    System.out.println("3. Seller");
    int option = in.nextInt();
    in.nextLine();
    
        if (option == 1) { 
            
            String user, pass;
            
            System.out.println("Enter your username: ");
            user = in.nextLine();
            
            System.out.println("Enter the password: ");
            pass = in.nextLine();
            
            if(user.equals("Admin1") || (pass.equals("Best"))) {
                System.out.println("test");
            } else { 
                System.out.println("Wrong password.");
            }
        }
    }
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading