4

when I get an input from the user I want to make sure that it is both:

  • a number
  • greater than a minimum value

I wrote the following code to achieve this but it seems more convoluted than it has to be. Is there a way to consolidate the question is the input a number and is that number less than ten, or any similar two part validation?

// function prompts user for a double greater than number passed in
// continues to prompt user until they input a number greater than
// the minimum number 
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 + ": ");
   } // end while

   userInput = scan.nextDouble();

   while (userInput <= minimumInput) {
      System.out.println("\nInvalid input.\n");
      userInput = getInput(minimumInput);
   }

   return userInput;
} // end getInput
Matt
  • 47
  • 3

2 Answers2

2

Simple answer: there is not.

You see, user input can be anything. If you would not be using that "nextDouble()" method your code would even have to do that conversion of strings into numbers. But there is no way in java to say: this thing is a double, and it must be smaller than some other value.

You explicitly have to "put down" that constraint into code. And the code you have right now is fine in that perspective. I even think it is better than the proposal within the other answer that tries to stuff all those tests into a single if condition.

You see, good code can be read and understood easily. Of course, "less code" is often quicker to read, but sometimes a "bit more" of code can be understood much quicker than the shorter version!

GhostCat
  • 137,827
  • 25
  • 176
  • 248
0

You can use || short circut OR operator to consolidate both validations as below:

public static double getInput(double minimumInput) {
           Scanner scan = new Scanner(System.in);
           double userInput =0;
           System.out.print("Enter a number greater than " + minimumInput + ": ");
           //Combine two vlidations using || operator
           while (!scan.hasNextDouble() ||  ((userInput=scan.nextDouble()) < minimumInput)){
              System.out.println("\nInvalid input.\n");
              System.out.print("Enter a number greater than " + minimumInput + ": ");
           } // end while
           return userInput;
        } // end getInput

Please refer the below link for more details on operators below: https://docs.oracle.com/javase/tutorial/java/nutsandbolts/op2.html

Vasu
  • 21,832
  • 11
  • 51
  • 67
  • I think this is going in the **wrong** direction. Good coding style is about **readability**. Thus: conditions that are *easy* to understand. And surprisingly, an if that uses NOT and OR and a method call and < is **not** easy to understand. – GhostCat Oct 28 '16 at 18:05
  • Thanks for the feed back, this was the _idea_ I was looking for. However, there is an error with your code. If I enter a number less than minimumInput it works fine but if I enter a string the code enters an infinite loop, because the parameter !scan.hasNextDouble evaluates to false and does not "clear" the scanner (not sure if I'm explaining that right). Do you have any ideas on how to fix that problem? – Matt Oct 28 '16 at 18:24