-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPrompt.java
86 lines (74 loc) · 1.78 KB
/
Prompt.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
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
// Karthik Bala
// Propmt.java
// August 22, 2012
// Contains methods that facilitate prompting for numerical (int and double) values.
import java.util.Scanner;
public class Prompt{
public static int getInt(String ask){
boolean badinput = false;
int value = 0;
do{
String input = getString(ask);
badinput = false;
try{
value = Integer.parseInt(input);
}
catch(NumberFormatException e){
badinput = true;
}
}
while(badinput);
return value;
}
public static int getInt(String ask, int min, int max){
int value;
System.out.println("\n");
do{
value = getInt(ask);
}
while(value < min || value > max);
return value;
}
public static String getString (String ask){
Scanner keyboard = new Scanner (System.in);
System.out.print(ask);
String input = keyboard.nextLine();
return input;
}
public static char getChar (String ask){
Scanner keyboard = new Scanner (System.in);
System.out.print(ask);
String input = keyboard.nextLine();
char inputChar = 0;
if (!input.isEmpty()){
inputChar = input.toCharArray()[0];
}
return inputChar;
}
public static double getDouble (String ask){
boolean badinput = false;
double value = 0.0;
do{
badinput = false;
String input = getString(ask);
try{
value = Double.parseDouble(input);
}
catch(NumberFormatException e){
badinput = true;
}
}
while(badinput);
return value;
}
public static double getDouble (String ask, double min, double max){
double value;
do{
String stringmin = String.format("%6.2f", min); //format the doubles to 2 decimal places
String stringmax = String.format("%6.2f", max);
value = getDouble(ask + "(" + stringmin + " - " + stringmax + ") -> ");
}
while(value < min || value > max);
return value;
}
}