-
Notifications
You must be signed in to change notification settings - Fork 1
/
InputTester.java
54 lines (47 loc) · 1.02 KB
/
InputTester.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
package BattleSimulator;
import java.util.Scanner;
public class InputTester
{
private static Scanner input = new Scanner(System.in);
public static int checkInteger()
{
while (true)
{
String x = input.next();
try
{
if (Integer.parseInt(x) >= 0)
return Integer.parseInt(x);
else
System.out.print("\nNumber must be >= 0. Try again: ");
}
catch (NumberFormatException e)
{
System.out.print("\n" + x + " is not an integer! Try again: ");
}
}
}
public static int checkInteger(int n)
{
while (true)
{
String x = input.next();
try
{
if (Integer.parseInt(x) > 0 && Integer.parseInt(x) <= n)
return Integer.parseInt(x);
else
System.out.printf("\nNumber must be > 0 and <= %d. Try again: ", n);
}
catch (NumberFormatException e)
{
System.out.print("\n" + x + " is not an integer! Try again: ");
}
}
}
public static String unchecked()
{
String x = input.next();
return x;
}
}