-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathKeyboardInputClass.java
More file actions
116 lines (108 loc) · 4.52 KB
/
Copy pathKeyboardInputClass.java
File metadata and controls
116 lines (108 loc) · 4.52 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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
import java.io.*;
class KeyboardInputClass {
static String getKeyboardInput(String prompt) {
String inputString = "";
System.out.println(prompt);
try {
InputStreamReader reader = new InputStreamReader(System.in);
BufferedReader buffer = new BufferedReader(reader);
inputString = buffer.readLine();
} catch (Exception e) {}
return inputString;
}
static char getCharacter(boolean validateInput, char defaultResult, String validEntries, int caseConversionMode, String prompt) {
if (validateInput) {
if (caseConversionMode == 1) {
validEntries = validEntries.toUpperCase();
defaultResult = Character.toUpperCase(defaultResult);
} else if (caseConversionMode == 2) {
validEntries = validEntries.toLowerCase();
defaultResult = Character.toLowerCase(defaultResult);
}
if ((validEntries.indexOf(defaultResult) < 0))
{
validEntries = (new Character(defaultResult)).toString() + validEntries;
}
}
String inputString = "";
char result = defaultResult;
boolean entryAccepted = false;
while (!entryAccepted) {
result = defaultResult;
entryAccepted = true;
inputString = getKeyboardInput(prompt);
if (inputString.length() > 0) {
result = (inputString.charAt(0));
if (caseConversionMode == 1) {
result = Character.toUpperCase(result);
} else if (caseConversionMode == 2) {
result = Character.toLowerCase(result);
}
}
if (validateInput) {
if (validEntries.indexOf(result) < 0) {
entryAccepted = false;
System.out.println("\nInvalid entry. Select an entry from the characters shown in brackets: [" + validEntries + "]");
}
}
}
return result;
}
static int getInteger(boolean validateInput, int defaultResult, int minAllowableResult, int maxAllowableResult, String prompt) {
String inputString = "";
int result = defaultResult;
boolean entryAccepted = false;
while (!entryAccepted) {
result = defaultResult;
entryAccepted = true;
inputString = getKeyboardInput(prompt);
if (inputString.length() > 0) {
try {
result = Integer.parseInt(inputString);
} catch (Exception e) {
entryAccepted = false;
System.out.println("\nInvalid entry. Input not recognized.");
}
}
if (entryAccepted && validateInput) {
if ((result != defaultResult) && ((result < minAllowableResult) || (result > maxAllowableResult))) {
entryAccepted = false;
System.out.println("\nInvalid entry. Allowable range is from " + minAllowableResult + " to " + maxAllowableResult + ".");
}
}
}
return result;
}
static double getDouble(boolean validateInput, double defaultResult, double minAllowableResult, double maxAllowableResult, String prompt) {
String inputString = "";
double result = defaultResult;
boolean entryAccepted = false;
while (!entryAccepted) {
result = defaultResult;
entryAccepted = true;
inputString = getKeyboardInput(prompt);
if (inputString.length() > 0) {
try {
result = Double.parseDouble(inputString);
} catch (Exception e) {
entryAccepted = false;
System.out.println("\nInvalid entry. Input not recognized.");
}
}
if (entryAccepted && validateInput) {
if ((result != defaultResult) && ((result < minAllowableResult) || (result > maxAllowableResult))) {
entryAccepted = false;
System.out.println("\nInvalid entry. Allowable range is from " + minAllowableResult + " to " + maxAllowableResult + ".");
}
}
}
return result;
}
static String getString(String defaultResult, String prompt) {
String result = getKeyboardInput(prompt);
if (result.length() == 0) {
result = defaultResult;
}
return result;
}
}