-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUserInput.java
More file actions
177 lines (160 loc) · 6.35 KB
/
UserInput.java
File metadata and controls
177 lines (160 loc) · 6.35 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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
import java.util.Scanner;
/**
* UserInput class implements generic methods for taking input from the user.
* Its two main methods are for reading a string from the user and reading an number from the user
*
* @author (Bhavik maneck)
* @version (v1)
*/
public class UserInput
{
final static String ESC = "\033[";
/**
* Constructor for UserInput
*/
public UserInput()
{
//Nothing to set or create
}
// Clears the terminal screen for user, requires console to support ANSI escape sequences
// Taken from: http://stackoverflow.com/questions/4888362/commands-in-java-to-clear-the-screen
public void clearScreen()
{
System.out.print(ESC + "2J");
}
/*
* Check if every character in a given string is numeric
*
* Takes as argument a string for checking
*
* Returns false to indicate string is not a numeric otherwise true to indicate it is numeric
*
*/
private boolean isStringNumeric(String stringToCheck)
{
for (int i = 0; i < stringToCheck.length(); i++) //Iterate through the string character by character and check if it is a number
{
//Consider one character at a time
String c = stringToCheck.substring(i,i+1);
//Check if this character is a number
if (!(c.equals("0") || c.equals("1") || c.equals("2") || c.equals("3") ||
c.equals("4") || c.equals("5") || c.equals("6") || c.equals("7") ||
c.equals("8") || c.equals("9")))
{
return false;
}
}
return true;
}
// Generic method that pauses execution until user presses enter
public void pressEnterToContinue()
{
Scanner console = new Scanner(System.in);
System.out.print("Press enter to continue...");
console.nextLine();
}
/*
* Generic method for reading a single integer from user between 1 and maxNumber
*
* Arguments:
* maxNumber - maximum number to accept, set maxNumber <= 0 if no check for maximum required
* inputPrompt - string to display to user when asking for a number
*
* If input is a not a valid number within the required range a 0 is returned to indicate error,
* otherwise valid int from user is returned
*
*/
public int readIntegerFromUser(int maxNumber, String inputPrompt)
{
Scanner console = new Scanner(System.in); // Create a scanner object read input from user
boolean error = true; //error is true to begin as we have no number from user initiall
int numberChosen = 0;
int inputNumber = -1;
System.out.print("\n"+inputPrompt); // Display the custom prompt specified asking for the input to the user
String input = console.nextLine(); // Read the line inputted by user
input = input.trim(); // Remove spaces from either end of the string
//First check if something was at least entered by user, otherwise error
if (input.length() == 0)
{
System.out.print("\nError - you must enter something!\n");
pressEnterToContinue();
error = true;
}
else
{
//Check if input is a number first
boolean inputIsNumber = isStringNumeric(input);
//If Input is a number check if its a valid number, otherwise tell user we need a number
if (inputIsNumber)
{
// We know string is a number so convert it to an integer now
inputNumber = Integer.parseInt(input);
// Only do more checks if a maximum number to check for has been set (maxNumber <= 0 indicates no check for maximum)
if (maxNumber > 0)
{
// Check that the input number is greater than 0 and less than or equal to the maximum provided
if ((inputNumber <= maxNumber) && (inputNumber > 0))
{
numberChosen = inputNumber;
error = false;
}
else
{
System.out.print("\nError! Entered number must be between 1 and " + maxNumber + "\n");
pressEnterToContinue();
error = true;
}
}
else
{
numberChosen = inputNumber;
error = false;
}
}
else
{
// Input string wasn't a number
System.out.print("\nError! Input must be a number!\n");
pressEnterToContinue();
error = true;
}
}
if (error)
{
// There was some error in the input from user, 0 is returned to indicate error
return 0;
}
else
{
// no error than return the number, guarunteed to be between 1 and maxNumber provided
return numberChosen;
}
}
/*
* Read an optionally non-empty string from user, with message prompt
*
* Takes as argument a string for checking
*
* Returns false to indicate string is not a numeric otherwise true to indicate it is numeric
*
*/
public String readStringFromUser(String messagePrompt, boolean emptyStringAllowed)
{
Scanner console = new Scanner(System.in); // Create a scanner object read input from user
System.out.print(messagePrompt);
String inputName = console.nextLine();
inputName = inputName.trim(); // Remove spaces from either end of the string so whitespace alone isn't accepted
if (!emptyStringAllowed)
{
while (inputName.length() == 0) // If user enters nothing for input keep asking for name until get back string of length greater than 0
{
clearScreen();
System.out.print("Error - input must be at least one character long.\n");
System.out.print(messagePrompt);
inputName = console.nextLine();
inputName = inputName.trim(); // Remove spaces from either end of the string
}
}
return inputName;
}
}