public class UserInput {
static Scanner input = new Scanner(System.in);
public static double getDouble() {
double value;
while(true) { // while
try {
value = input.nextDouble();
break;
}
catch (Exception e) {
System.out.print("Error! Please re-enter double value: ");
input.next();
}
}
input.nextLine();
return value;
}
public static int getIntegerBetween(int min, int max) {
int value = 0;
do { // do-while
System.out.printf("Enter the number between [%d - %d]", min, max);
try {
value = input.nextInt();
}
catch (Exception e) {
System.out.println("Error! Please re-enter integer value");
input.next();
continue;
}
} while(value < min || value > max);
return value;
}
public static boolean getUserExitKey()
{
System.out.println("Press q-key to quit the program or enter-key to exit the program");
String s = input.nextLine();
if (s.contentEquals("q")) {
return true;
}
else {
return false;
}
}
}