I try to write a programm in Java that gets user input using Scanner Class. The user has to enter any positive integer number. Depending on user actions, the results should be as follows:
The user has entered not an integer number -> the programm prints the message
Oops! You entered something different, but not an integer number, try again
The user has entered not a positive integer number -> the programm prints the message
You entered not a positive integer number, try again
The user has entered a positive integer number -> the programm prints the number
User's positive integer number - ...
I have written some code using loop and Scanner class
public static void main(String[] args) {
int userIntNum;
boolean isUserInputCorrect;
@SuppressWarnings("resource")
Scanner sc = new Scanner(System.in);
userIntNum = 0;
System.out.println("Please, enter a positive integer number");
isUserInputCorrect = (sc.hasNextInt() && (userIntNum > 0));
// double console input for correct integer number
while (isUserInputCorrect == false) {
if (!sc.hasNextInt()) {
System.out.println("Oops! You entered something different, but not an integer number, try again");
sc.nextLine();
} else if (sc.nextInt() <= 0) {
System.out.println("You entered not a positive integer number, try again");
sc.nextLine();
} else {
break;
}
}
userIntNum = sc.nextInt();
System.out.println("User's positive integer number - " userIntNum);
When I put in the console a positive integer number (the correct input), the programm, for some reason, asks me to enter this number twice. Moreover, if I put first an integer number and then any non-positive number separated by space, it will print this incorrect number. And if I put first an integer number and then not an integer number separated by space, it will throw an exception.
Why does it happen and how can I fix these errors?
CodePudding user response:
First, I would eliminate isUserInputCorrect
. You are trying to do too much with it, instead I would loop while userIntNum
is less than or equal to zero. Also, try and limit variable scope. Something like,
Scanner sc = new Scanner(System.in);
int userIntNum = -1;
System.out.println("Please, enter a positive integer number");
// double console input for correct integer number
while (userIntNum <= 0) {
if (sc.hasNextInt()) {
userIntNum = sc.nextInt();
} else {
System.out.println("Oops! You entered something different, "
"but not an integer number, try again");
sc.nextLine();
}
if (userIntNum <= 0) {
System.out.println("You entered not a positive integer number, try again");
}
}
System.out.println("User's positive integer number - " userIntNum);
CodePudding user response:
Expanding on Elliott's answer above, I wanted to provide an alternative solution that addresses your following point:
Moreover, if I put first an integer number and then any non-positive number separated by space, it will print this incorrect number. And if I put first an integer number and then not an integer number separated by space, it will throw an exception.
The Scanner class will read tokens individually in a temporal fashion. If you look at the nextInt()
function you will see it that throws InputMismatchException
which you can explicitly catch.
Additionally, take a look at the Java modulus operator. Since you are explicitly looking for even values, the modulus operator is extremely valuable here.
public static void main(String[] args) {
int userIntNum = 0;
boolean isUserInputCorrect = false;
@SuppressWarnings("resource")
Scanner sc = new Scanner(System.in);
do {
System.out.println("Please, enter a positive integer number");
try {
userIntNum = sc.nextInt();
isUserInputCorrect = (userIntNum > 0 && userIntNum % 2 == 0);
} catch (InputMismatchException ex) {
System.out.println("Invalid input: please enter an integer value");
sc.next();
}
} while(!isUserInputCorrect);
System.out.println("User's positive integer number - " userIntNum);
}