-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInputHandler.java
More file actions
54 lines (46 loc) · 1.51 KB
/
InputHandler.java
File metadata and controls
54 lines (46 loc) · 1.51 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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import java.util.Scanner;
public class InputHandler {
public static int ObtainIntInput(Scanner scanner, int messageType)
{
while ( true ) {
String userInput = scanner.nextLine();
if (VerifyInput(userInput)) {
int value = Integer.parseInt(userInput);
if (value < 0) {
System.out.println("Negative numbers are not allowed; please enter a non-negative integer.");
} else {
return value;
}
} else {
System.out.println("That is not an acceptable input; please type an integer.");
if ( messageType == 1 ) {
System.out.print("Selection: ");
}
}
}
}
public static boolean VerifyInput(String input)
{
try {
Integer.parseInt(input);
return true;
} catch (NumberFormatException e) {
return false;
}
}
public static String ObtainGameInput(Scanner scanner)
{
while ( true ) {
String userInput = scanner.nextLine();
if ( VerifyInput(userInput) ) {
return userInput;
} else {
if ( userInput.equals("end")) {
return userInput;
} else {
System.out.println("That is not an acceptable input; please type an integer.");
}
}
}
}
}