-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUtils.java
More file actions
30 lines (26 loc) · 1.01 KB
/
Utils.java
File metadata and controls
30 lines (26 loc) · 1.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
package utils;
import java.util.InputMismatchException;
import java.util.Scanner;
public class Utils {
public enum UserType {
ADMIN, PLAYER
}
public static int getValidInteger(int lowerLimit, int upperLimit) {
Scanner scanner = new Scanner(System.in);
int userChoice = 0;
boolean isAnInt = false;
do {
try {
userChoice = scanner.nextInt();
isAnInt = true;
if (userChoice < lowerLimit || userChoice > upperLimit) {
System.out.println("Error: Please enter a valid number between " + lowerLimit + " and " + upperLimit + ".");
}
} catch (InputMismatchException ime) {
scanner.nextLine(); // Consume the invalid input and reset
System.out.println("Invalid input (not a number). Please enter a valid number.");
}
} while (userChoice < lowerLimit || userChoice > upperLimit || !isAnInt);
return userChoice;
}
}