0

I have a hard time to figure out how to use the Scanner correctly to validate the user input. For example, the program need to have a user input of int, user can only input positive number, and if the user input "apple","delicious apple",or negative number, the problem will show error message. I have tried the code below, and then i found a disturbing questions, the "That's not a number!" are printed twice, I have no idea what caused this questions....

import java.util.*;
public class input {
    public static void main(String[] args){

        Scanner sc = new Scanner(System.in);
        int number;

        do {
            System.out.println("Please enter a positive number!");
            while (!sc.hasNextInt()) //scan
            {
                System.out.println("That's not a number!");
                sc.nextLine();  //scan
            }
            number = sc.nextInt();

        }
        while (number <= 0);

        System.out.println("Thank you! The positive number is " + number);
    }
}

Here is the result:
Please enter a positive number!
dfd
That's not a number!
-2232
Please enter a positive number!
dfd dfd
That's not a number!
That's not a number!

And I accidently solve the issue by putting extra line of code below the "number = sc.nextInt()", and now my code became:

import java.util.*;

public class input {
    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);
        int number;

        do {
            System.out.println("Please enter a positive number!");
            while (!sc.hasNextInt()) //scan
            {
                System.out.println("That's not a number!");
                sc.nextLine();  //scan
            }
            number = sc.nextInt();
            sc.nextLine();  //scan

        }
        while (number <= 0);

        System.out.println("Thank you! The positive number is " + number);
    }
}

Here is the result:
Please enter a positive number!
dfd
That's not a number!
-2323
Please enter a positive number!
dfd dfd
That's not a number!
fdfd 322
That's not a number!
23 sdfd
Thank you! The positive number is 23

this time the "That's not a number" printed only once, but I really can't see why by putting "sc.nextLine()" helps solves the problem.....

and still, I come another question, what if I also want to validate the user input when they input something like "23 sdfd","23 2323" and the problem still provide that the information to prompt the user to try again until they input correct int?

Ashot Karakhanyan
  • 2,734
  • 3
  • 21
  • 28
  • 1
    is a "positive number" only a positive integer? Do you not consider doubles for instance? – Adeeb Apr 11 '14 at 19:37
  • +1 @Adeeb also, it wont work if the number is`long` rather an `int` – maxx777 Apr 11 '14 at 19:42
  • You seem to be confused about whether you are writing the code or the code is writing itself. It's your code you should control how it behaves (at any situation). – Bhesh Gurung Apr 11 '14 at 19:46
  • for the first code, when you enter -2232, the flow moves out of the while loop. After that you enter another value using `nextline()`. In your input you have entered a string. After that you again enter another value when the do is re-iterated. So the `Scanner` is holding two values. because of this, the output is printed twice – maxx777 Apr 11 '14 at 19:59

1 Answers1

0

It's because number = sc.nextInt(); did not consume the newline character after you entered -2232. So it evaluated that newline character, determined that it's not an int, and prompted "That's not a number!". Then you entered dfd dfd, it determined that that was not an int, and prompted "That's not a number!" again.

Your edit fixed the problem by consuming the newline character after it read in the int.

see Scanner is skipping nextLine() after using next(), nextInt() or other nextFoo() methods

Community
  • 1
  • 1
anycard
  • 301
  • 1
  • 10