-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathInputChecks.java
74 lines (51 loc) · 3 KB
/
InputChecks.java
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
// Software Engineering Task 24 - Capstone Project III
import java.util.Scanner;
// PoisedInputChecks is the Poised Project superclass.
// This class contains three check methods for different user inputs.
// These methods are inherited by the three subclasses of the program to ensure all user input is correctly checked.
public class InputChecks { // Main class declaration.
// This method verifies user input when asked to enter an integer
// @param type describes the input required for the user to enter
// @return returns the verified output integer
public static int intCheck(String type) {
while(true) { // While loop repeatedly re-prompts for input until correct.
Scanner numInput = new Scanner(System.in);
String number = numInput.nextLine();
try {
int output = Integer.parseInt(number); // Attempting to parse the input to an integer (i.e. checking for correct input).
return output;
} catch (NumberFormatException ex) {
System.out.println("Incorrect entry. Please re-enter the " + type + ": \n"); // Error message displayed if parsing is not possible.
}
}
}
// This method verifies user input when asked to enter a string.
// @param type describes the input required for the user to enter
// @return returns the verified user input
public static String stringCheck(String type) {
while(true) { // While loop repeatedly re-prompts for input until correct.
Scanner userInput = new Scanner(System.in);
String input = userInput.nextLine();
if ((input == null) || (input.length() > 150)) { // Checking if the input is empty, too short, or too long.
System.out.println("Incorrect entry. Please re-enter the " + type + ": \n");
} else {
return input; // Returning the user's correctly input string.
}
}
}
// This method verifies user input when asked to enter a double number.
// @param type describes the input required for the user to enter
// @return returns the verified output double
public static double doubleCheck(String type) {
while(true) { // While loop repeatedly re-prompts for input until correct.
Scanner doubleInput = new Scanner(System.in);
String number = doubleInput.nextLine();
try {
double output = Double.parseDouble(number); // Attempting to parse the input to a double (i.e. checking for correct input).
return output;
} catch (NumberFormatException ex) {
System.out.println("Incorrect entry. Please re-enter the " + type + ": \n"); // Error message displayed if parsing is not possible.
}
}
}
}