-2

I'm trying to verify user input for a school assignment, wherein a negative number or anything except for a double causes the loop to retry. The rest of the code does some simple arithmetic with the input and that's all functional so I didn't include it, but here is the input verification I have so far. Right now inputting a negative number does what I want it to do, but entering a different character causes in infinite loop of

"null

Enter wall height (feet): "

Why isn't the !scnr.hasNextDouble() exception throwing, and why is it repeating infinitely, when the negative number exception is working as intended? I'd very much appreciate any help I could get on this, thank you.

public static void main(String[] args) {
    Scanner scnr = new Scanner(System.in);
    double wallHeight = 0.0;
    double wallWidth = 0.0;
    double wallArea = 0.0;
    double gallonsPaintNeeded = 0.0;

    final double squareFeetPerGallons = 350.0;


    // Implement a do-while loop to ensure input is valid  

    do {
        try {
            System.out.println("Enter wall height (feet): ");
            wallHeight = scnr.nextDouble();
            if (wallHeight <= 0) {
                throw new Exception("Enter positive number");
            }
            else if (!scnr.hasNextDouble()) {
                throw new Exception("Invalid input.");
            }
            }
        catch(Exception excpt){
            System.out.println(excpt.getMessage());
        }
        }while(wallHeight <= 0 || !scnr.hasNextDouble());
  • Welcome to Stack Overflow. ___Before asking___ any question you're expected to ___search for already existing Q/As___... like [How to use java.util.Scanner to correctly read user input from System.in and act on it?](https://stackoverflow.com/questions/26446599/how-to-use-java-util-scanner-to-correctly-read-user-input-from-system-in-and-act). Also note that the community here doesn't replace other great online resources such as tutorials. For reference, have a look at [How to Ask a Good Question](https://stackoverflow.com/help/how-to-ask). – Ivo Mori Jun 13 '20 at 08:14

1 Answers1

0

You need to make sure the input is a double and not a String or an Integer. Flip the order of the if and else-if, first of all, check if the input is even a double, and only then check if it's negative.

Alternatively, you can use a Regex to validate your input. The Regex below checks for any number between 0 to 9, followed by a period and another number from 0 to 9. Note that it works for multiple digits as well.

^[0-9]+\.[0-9]+$

Simply use a String.matches(regex) to validate your input.

If you'd like clarification of what each symbol do: The ^ signifies the beginning of a line, meaning that nothing shall come before it. Equally, the $ signifies the end of a line, meaning that nothing shall come after it.

The [0-9] is the range of valid characters, in this case, 0 to 9.

The + signifies that there can be more than one of these characters (multiple digits).

Then we have the period, accompanied by an escape character \ since a . on its own in a Regex means "any character", hence we have to escape it.

If you'd like to play and learn about Regex more, I suggest you visit this site.

Eldar B.
  • 1,097
  • 9
  • 22