简体   繁体   中英

Java - If statement logic error

The sequence of events should be:

1) I enter two.
2) I am prompted to choose a name.
3) I choose either Washington/ Franklin/ Hamilton.
4) I'm asked which denomination does this name appear on.
5) I give the answer.

However, when I enter Washington for part three - I am told that is an invalid number. I cannot see why this would be.

public static void main(String[] args) {
    // TODO code application logic here
    System.out.println("Type 1 to enter a denomination, 2 to enter a last name");
    Scanner in = new Scanner(System.in);
    int x = in.nextInt();

    if(x==1){
        System.out.println("Choose a denomination");
        int y = in.nextInt();
        in.nextLine();
        if(y==1){
            System.out.println("Which person appears on the 1 bill?");
            String answer = in.nextLine();
            if(answer.equals("Washington")){
                System.out.println("That is correct");
            }
            else{
                System.out.println("That is incorrect");
            }
        }


        else if(y==10){
            System.out.println("Which person appears on the 10 bill?");
            String answer = in.nextLine();
            if(answer.equals("Hamilton")){
                System.out.println("That is correct");
            }
            else{
                System.out.println("That is incorrect");
            }
        }
        else if(y==100){
            System.out.println("Which person appears on the 100 bill?");
            String answer = in.nextLine();
            if(answer.equals("Franklin")){
                System.out.println("That is correct");
            }
            else{
                System.out.println("That is incorrect");
            }
        }
        else{
            System.out.println("That is an invalid number.");
        }
    }
    else if(x==2){
       System.out.println("Choose a name");
        String y = in.nextLine();
        in.nextLine();
        if(y.equals("Washington")){
            System.out.println("Which denomination does this name appear on?");
            int answer = in.nextInt();
            if(answer==1){
                System.out.println("That is correct");
            }
            else{
                System.out.println("That is incorrect");
            }
        }


        else if(y.equals("Hamilton")){
            System.out.println("Which denomination does this name appear on");
            int answer = in.nextInt();
            if(answer==10){
                System.out.println("That is correct");
            }
            else{
                System.out.println("That is incorrect");
            }
        }
        else if(y.equals("Franklin")){
            System.out.println("Which denomination does this name appear on");
            int answer = in.nextInt();
            if(answer==100){
                System.out.println("That is correct");
            }
            else{
                System.out.println("That is incorrect");
            }
        }
        else{
            System.out.println("That is an invalid number.");
        }
    } 
}

The problem is with the x==2 segment. x==1 works fine.

The problem does not lies with the seemingly extra in.nextLine() .

I will advise you to change all your in.nextInt() to in.nextLine() followed by parsing them to the actualy type such as int

Example:

int answer = Integer.parseInt(in.nextLine());

Reason for the suggested change : When you use nextInt() , there is a tendency that nextline still lingers around there until you do an additional nextLine() to clear it.

To prevent this sort of problems, it is advisable to receive all inputs with nextLine() , then parse it to the actual type ( int , double ..etc).

This is also how Microsoft deals with integer input in C# .


Edited Working Codes:

public static void main(String[] args) {

    System.out.println("Type 1 to enter a denomination, 2 to enter a last name");
    Scanner in = new Scanner(System.in);
    int x = Integer.parseInt(in.nextLine());

    if(x==1){
        System.out.println("Choose a denomination");
        int y = Integer.parseInt(in.nextLine());
        in.nextLine();
        if(y==1){
            System.out.println("Which person appears on the 1 bill?");
            String answer = in.nextLine();
            if(answer.equals("Washington")){
                System.out.println("That is correct");
            }
            else{
                System.out.println("That is incorrect");
            }
        }


        else if(y==10){
            System.out.println("Which person appears on the 10 bill?");
            String answer = in.nextLine();
            if(answer.equals("Hamilton")){
                System.out.println("That is correct");
            }
            else{
                System.out.println("That is incorrect");
            }
        }
        else if(y==100){
            System.out.println("Which person appears on the 100 bill?");
            String answer = in.nextLine();
            if(answer.equals("Franklin")){
                System.out.println("That is correct");
            }
            else{
                System.out.println("That is incorrect");
            }
        }
        else{
            System.out.println("That is an invalid number.");
        }
    }
    else if(x==2){
       System.out.println("Choose a name");
        String y = in.nextLine();
        //in.nextLine();
        if(y.equals("Washington")){
            System.out.println("Which denomination does this name appear on?");
            int answer = Integer.parseInt(in.nextLine());
            if(answer==1){
                System.out.println("That is correct");
            }
            else{
                System.out.println("That is incorrect");
            }
        }


        else if(y.equals("Hamilton")){
            System.out.println("Which denomination does this name appear on");
            int answer = Integer.parseInt(in.nextLine());
            if(answer==10){
                System.out.println("That is correct");
            }
            else{
                System.out.println("That is incorrect");
            }
        }
        else if(y.equals("Franklin")){
            System.out.println("Which denomination does this name appear on");
            int answer = Integer.parseInt(in.nextLine());
            if(answer==100){
                System.out.println("That is correct");
            }
            else{
                System.out.println("That is incorrect");
            }
        }
        else{
            System.out.println("That is an invalid number.");
        }
    } 
}
String y = in.nextLine();
in.nextLine();
if(y.equals("Washington")){

You're calling in.nextLine() one too many times. Remove that second line.

You have to skip the line after nextInt() then take user input(in y==2 case) because nextInt() doesnot consume the whole line it consume the token.

in.nextLine();
String y = in.nextLine();

with your code the in.nextLine() in String y = in.nextLine(); is reading the same line where you entered value of x getting the value y to be "" and hence the output "That is an invalid number"

When you do String y = in.nextLine() , the next token is the leftover newline from the previous nextInt() call, so y ends up being equal to \\n .

Place a call to nextLine() after you use nextInt() :

int x = in.nextInt();
in.nextLine();

Afterwards you should be able to call nextLine only once when getting the user input:

String y = in.nextLine();
if(y.equals("Washington")){

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