when I get input from a user, I want to make sure that both of them:
- number
- more than the minimum value
For this, I wrote the following code, but it seems more confusing than it should be. Is there a way to consolidate the question, is the input a number and is this number less than ten or any similar two-part check?
public static double getInput(double minimumInput) {
Scanner scan = new Scanner(System.in);
double userInput;
System.out.print("Enter a number greater than " + minimumInput + ": ");
while (!scan.hasNextDouble()){
String garbage = scan.next();
System.out.println("\nInvalid input.\n");
System.out.print("Enter a number greater than " + minimumInput + ": ");
}
userInput = scan.nextDouble();
while (userInput <= minimumInput) {
System.out.println("\nInvalid input.\n");
userInput = getInput(minimumInput);
}
return userInput;
}
source
share