-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInputProcessor.java
More file actions
33 lines (28 loc) · 1.06 KB
/
InputProcessor.java
File metadata and controls
33 lines (28 loc) · 1.06 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
31
32
33
import java.util.Scanner;
public class InputProcessor {
private static Scanner scanner = new Scanner(System.in);
// handle when people give out of bound numbers via InputProcessor by giving
// parameters
public static int getInt(int low, int high) {
try {
int choice = Integer.parseInt(scanner.nextLine());
if (choice >= low && choice <= high) {
return choice;
} else {
ConsoleStyler.printRedBold("Please Enter a Number within range: ");
return getInt(low, high);
}
} catch (NumberFormatException e) {
ConsoleStyler.printRedBold("Please Enter a Number: ");
return getInt(low, high);
}
}
public static String getString() {
String nextString = scanner.nextLine();
if (nextString.equals("") || nextString.trim().isEmpty()) {
System.out.print("Please Enter a valid input:");
return getString();
}
return nextString;
}
}