-5
public static int intInput(String prompt, String error) {

    int intInput = 0;  // Variable to be returned

    // Start the scanner
    Scanner keyboard = new Scanner(System.in);

    // This loop check to make sure the user entered positive data
    do {
        System.out.println(prompt);

        // This loop check if the user has entered an int
        while (!keyboard.hasNextInt()) {
            System.out.println(error);
            keyboard.next(); 
        }

        intInput = keyboard.nextInt();

    } while (intInput <= 0);

    // Returns an int
    return intInput;

}

If the user presses enter without entering any data, the prompt won't show back up. It just goes to the next line and it won't give any indication that the user can still enter data.

I changed next to nextLine, but I still get the same error

public static int intInput(String prompt, String error) {

    int intInput = 0;  // Variable to be returned

    // Start the scanner
    Scanner keyboard = new Scanner(System.in);

    // This loop check to make sure the user entered positive data
    do {
        System.out.println(prompt);

        // This loop check if the user has entered an int
        while (!keyboard.hasNextInt()) {
            System.out.println(error);
            keyboard.nextLine(); 
        }

        intInput = keyboard.nextInt();

    } while (intInput <= 0);

    // Returns an int
    return intInput;

}

Okay, so I read through the link and tried the using the nextLine deal, but it still didn't change anything

// Method for user int input
public static int intInput(String prompt, String error) {

    int intInput = 0;  // Variable to be returned

    // Start the scanner
    Scanner keyboard = new Scanner(System.in);

    // This loop check to make sure the user entered positive data
    do {
        System.out.println(prompt);

        // This loop check if the user has entered an int
        while (!(keyboard.hasNextInt())) {
            System.out.println(error);
            keyboard.nextLine(); 
        }

        intInput = keyboard.nextInt();
        keyboard.nextLine();
    } while (intInput <= 0);

    // Returns an int
    return intInput;

}
  • 3
    That is because 'next()' returns the next *token*, skipping any leading whitespace such as the newline. Use `nextLine()` if you want line-processing, instead of token-processing. See also: [Scanner is skipping nextLine() after using next() or nextFoo()?](https://stackoverflow.com/q/13102045/5221149) – Andreas Jun 25 '18 at 16:06
  • I still get the same problem – Andrew William Broughton Jun 25 '18 at 16:07
  • I'm confused why you're passing in an `error` here; usually, that would be thrown by the function itself. – Josh Katofsky Jun 25 '18 at 16:08
  • @JoshKatofsky The `error` parameter is the text to show the user when user enters a non-integer, so the message can be customized. – Andreas Jun 25 '18 at 16:09
  • @AndrewWilliamBroughton Edit the question and show what you tried. "Same problem" doesn't tell us what you did wrong. – Andreas Jun 25 '18 at 16:10
  • Okay, I added the new code – Andrew William Broughton Jun 25 '18 at 16:11
  • @Andreas, ah. ok. makes sense. I've never seen it done that way but to each their own :) – Josh Katofsky Jun 25 '18 at 16:12
  • 1
    Now, did you read through the link I provided? It's about the pitfalls of mixing `nextLine()` with `nextInt()`, and you fell right in. – Andreas Jun 25 '18 at 16:15
  • I read through it and changed some stuff, I added the new code, but I still get the same problem. I'm sorry if I'm being stupid, but I'm still really new to java and a lot of this is jargon to me. – Andrew William Broughton Jun 25 '18 at 17:00

1 Answers1

0

You could put in a try...catch in your loop, and just catch the exception if the user doesn't enter an int:

public static int intInput(String prompt, String error) {

    int intInput = 0;
    Scanner sc = new Scanner(System.in);
    do {
        try {
            System.out.println("Enter an int");
            intInput = Integer.parseInt(sc.nextLine());
        } catch (Exception e) {
            System.out.println(error);
            intInput = 0;
        }
    } while (intInput == 0);

    return intInput;
}

You might want to do some proper exception handling though, not catching the generic Exception but this is more of a guideline. By forcing intInput to 0 in the catch, this will mean it will not break out of the do...while and keep going until a valid int is entered.

achAmháin
  • 3,944
  • 3
  • 12
  • 39