0

I am hoping this is an easy fix. I am after a way to detect that the user has hit the enter key when prompted for an int value. I know the .isEmpty does not work with values declared as int, what is the best way to get around this?

System.out.println("Please enter the first number:");
    user_number1 = input.nextInt();
        if (user_number1.isEmpty){

        }
Alexx V
  • 3
  • 1

3 Answers3

3

There is no way for an int to be empty. input.nextInt() will not proceed until the user enters a value that is not whitespace. If it's not an int, it will throw a InputMismatchException. This is documented in the Scanner.nextInt() Javadoc. You can test if there is an int with Scanner.hasNextInt() before trying to consume the next token.

while (true) {
    System.out.println("Please enter the first number:");
    if (input.hasNextInt()) {
        user_number1 = input.nextInt();
        break;
    } else {
        System.out.println("not an int: " + input.nextLine());
    }
}
Elliott Frisch
  • 183,598
  • 16
  • 131
  • 226
0

As @Elliott Frisch's answer states, the .nextInt() call is just not going to return until an actual number of some sort is entered (or, if something else is submitted by the user, the InputMismatchException occurs instead.

One easy alternative is to just.. not call .nextInt() then. call .next(), check if the resulting String is empty, and if not, turn it into an integer using: int userNumber = Integer.parseInt(theStringYouGotFromScannerNext);.

NB1: Java convention states a variable is named 'userNumber1', not 'user_number1'. When in Rome and all that.

NB2: If you want your scanner to read 1 answer every time the user presses enter, call scanner.useDelimiter("\r?\n"); immediately after new Scanner. Out of the box it gives you 1 answer per whitespace which is usually not what you want in the first place.

rzwitserloot
  • 44,252
  • 4
  • 27
  • 37
0

I suggest you use nextLine() instead of using nextInt(), next() etc. Check Scanner is skipping nextLine() after using next() or nextFoo()? to learn more about it.

import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        int first = getInteger(input, "Please enter the first number:");
        System.out.println("You entered " + first);
        int second = getInteger(input, "Please enter the second number:");
        System.out.println("You entered " + second);
    }

    static int getInteger(Scanner input, String prompt) {
        boolean valid;
        int n = 0;
        do {
            valid = true;
            System.out.print(prompt);
            try {
                n = Integer.parseInt(input.nextLine());
            } catch (NumberFormatException e) {
                System.out.println("This is not an integer. Please try again.");
                valid = false;
            }
        } while (!valid);
        return n;
    }
}

A sample run:

Please enter the first number:a
This is not an integer. Please try again.
Please enter the first number:10.5
This is not an integer. Please try again.
Please enter the first number:
This is not an integer. Please try again.
Please enter the first number:10
You entered 10
Please enter the second number:
This is not an integer. Please try again.
Please enter the second number:40.5
This is not an integer. Please try again.
Please enter the second number:20
You entered 20
Arvind Kumar Avinash
  • 50,121
  • 5
  • 26
  • 72