-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIOHelper.java
More file actions
55 lines (42 loc) · 1.53 KB
/
IOHelper.java
File metadata and controls
55 lines (42 loc) · 1.53 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
import java.util.InputMismatchException;
import java.util.Scanner;
public class IOHelper
{
private static Scanner input = new Scanner(System.in);
public static int getInt(int low, String prompt, int high)
{
int response = 0;
String garbage;
boolean numericEntryOK;
do
{
System.out.print(prompt);
numericEntryOK = false;
try
{
// get the user's response
response = input.nextInt();
garbage = input.nextLine();
numericEntryOK = true;
}
catch (InputMismatchException e)
{
// if the user's response is not an integer
garbage = input.nextLine();
System.out.println(garbage + " is not an integer!");
response = low;
} // end try-catch
// if the user's input is outside the boundaries
if (response < low || response > high)
System.out.println("The number is outside the legal limits.");
} while (numericEntryOK == false || response < low || response > high); // end loop
return response;
} // end getInt
public static String getString(String prompt)
{
String response;
System.out.print(prompt);
response = input.nextLine();
return response;
} // end getString method
} // end IOHelper class