-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIntegerInput.java
More file actions
56 lines (48 loc) · 1.89 KB
/
IntegerInput.java
File metadata and controls
56 lines (48 loc) · 1.89 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
import java.util.*;
/*
@param input Scanner Instance
@param prompt Message shown to user
@param min Minimum valid integer (inclusive)
@param max Maximum valid integer (inclusive)
@return Valid user integer input
*/
public class IntegerInput
{
public static int promptInt(Scanner input, String prompt, int min, int max) {
System.out.println(prompt);
System.out.print("> ");
boolean gaveValid = false;
int returnValue = 0;
if (max <= min) {
throw new IllegalArgumentException("Maximum parameter (" + max + ") can not be equal to or less than minimum (" + min + ").");
}
while (!gaveValid) {
if (input.hasNextInt()) {
returnValue = input.nextInt();
input.nextLine();
if (returnValue < min) {
System.out.println("[ERROR] The inputted value is less than the minimum. (" + returnValue + "<" + min + ")");
System.out.print("> ");
} else if (returnValue > max) {
System.out.println("[ERROR] The inputted value is greater than the maximum. (" + returnValue + ">" + max + ")");
System.out.print("> ");
} else {
gaveValid = true;
}
} else {
System.out.println("[ERROR] The inputted value is not an integer.");
input.nextLine();
System.out.print("> ");
gaveValid = false;
}
}
return returnValue;
}
public static void main(String[] args)
{
Scanner input = new Scanner(System.in); // put this in main
int int1 = promptInt(input, "Please enter an integer between 1 and 20, inclusive.", 1, 20);
System.out.println(int1);
input.close();
}
}