-2

Im trying to ask the user for two numbers. I want to check if those inputs are in fact numbers but the code I have so far does not let me enter a second value if the first input is a string.

So the scanner does not read anything the else statement.

How could I make it work?

import java.util.Scanner;

public class calculations {
    public static void main(String[] args) {
        Scanner console = new Scanner(System.in);
        System.out.print("Please enter your first name: ");
        String fname = console.nextLine();
        System.out.print("Please enter your last name: ");
        String lname = console.nextLine();
        System.out.print("Please enter your first number: ");
        if (console.hasNextInt()) {
            int number1 = console.nextInt();
            System.out.print("Please enter your second number: ");
            if (console.hasNextInt()) {
                int number2 = console.nextInt();
            }
        } else
            System.out.print("Please enter your second number: ");
        if (console.hasNextInt()) {
            int number2 = console.nextInt();
            // this part does not work
        }
    }
}
Hovercraft Full Of Eels
  • 276,051
  • 23
  • 238
  • 346
kiaora
  • 67
  • 7
  • 2
    Your `else` block contains a only a print statement. – PM 77-1 Nov 03 '17 at 22:37
  • 1
    Adding to previous comment, putting `else` condition in a `{ }` block should help. – Eugene Nov 03 '17 at 22:45
  • I have tried that already. Sadly it doesn't help. Any other ideas? – kiaora Nov 03 '17 at 22:52
  • `if (condition) else if (same condition)` doesn't make sense: if the condition was false in the `if` block, it will also be false in the `else if` block. You just need to think about what you really want to do (i.e. read what the user has entered, even if it's not an int, and then ask again). Read the javadoc of Scanner. – JB Nizet Nov 03 '17 at 23:19

1 Answers1

1

You just need to add console.nextLine(); after your else statement, because the Scanner.hasNextInt method does not move cursor past your previous input (if it is a string).

Eugene
  • 1,357
  • 1
  • 15
  • 17
  • 1
    The OP never calls nextInt(). And the problem you're referring to is not about that. It's about nextLine() reading an empty string because nextInt() doesn't consume the EOL. Calling next() would be the wrong solution to this problem, but this problem doesn't occur here. – JB Nizet Nov 04 '17 at 00:11
  • @JB Nizet, you are right, the problem is not about consuming EOL. But nevertheless moving the cursor with the next() method seems to be a valid solution, so I edited the answer. Thanks for pointing it out! – Eugene Nov 04 '17 at 00:20
  • 1
    Where will the cursor be if the answer to "Please enter your first number: " is "Hello world", and you call next() to move the cursor? Where should it be? – JB Nizet Nov 04 '17 at 00:22
  • 1
    It's a specific input, but I agree with you that `nextLine` method would be a better option for the answer. – Eugene Nov 04 '17 at 00:28
  • Thank you very much for your helpful feedback. :) I have one more question though. Why does this part `if (console.hasNextInt()) { int number1 = console.nextInt();` allow an input. But this part doesn't? `else { if (console.hasNextInt()) { int number2 = console.nextInt(); }` – kiaora Nov 04 '17 at 16:25