0

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.

1

4 Answers 4

1

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.");
        }
    } 
}
Sign up to request clarification or add additional context in comments.

5 Comments

I've tried parsing all the ints to nextLine ~in segment two x==2, however the problem still persists.
@PeterMarker I already tried it and is working perfectly at my side. Copy and paste my codes and try again.
@PeterMarker After you tried my codes, let me know if it works on your side.
Worked like a charm. Thanks for the information, the kind of of issue I had isn't the type which I would have ever been thought. Should aid me a lot going forward.
@PeterMarker Yes, in future just receive input and parse it. There are no known side effects for doing this. If you use the additional nextLine() approach, there is still a tendency it may give you trouble which can cause you hours to spot the source of problem when you are unaware of it.
0
String y = in.nextLine();
in.nextLine();
if(y.equals("Washington")){

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

3 Comments

Ahhh. Due to the x==1 segment, I had the extra line. Forgot to remove it. Cheers
Hmm. Even when I remove the line, I get the "That is an invalid number" output before I even get to enter a name.
@PeterMarker Look at my solution, 100% can solve all your problems including your future problems of this sort.
0

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

Comments

0

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"

Comments

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.