Denzel Simson
Denzel Simson

Reputation: 33

Checking input to see if its a double in Java

I am working on creating code that asks for 2 numbers and then adds them up/multipies/divides. I managed to get this to work but I encountered a small problem.

This is the part of the code thats bugging me:

double number1 = 0;
do {
    try {
        System.out.print("First number: ");
        number1 = input.nextDouble();
    } catch (InputMismatchException e) {
        System.out.print("This is not a number. ");
    }
    input.nextLine();
} while (number1 == 0);

It works fine for what I am intending to do, which is ask for a number. If the user does not input a number, it catches the exception and prevents it from just exiting.

while (number1 == 0);

This is what I used to test if the try and catch worked as intended, but of course means number1 can not be 0 or it will not stop and keep asking for a number.

I want it to keep asking for input as long as a double is not entered, what do I replace?

Because this is a assigment, I have to keep asking for a double, if its better not to use the exception, that is acceptable. But it has to assign a number to number1 that I can use later on in the program

Upvotes: 1

Views: 64

Answers (2)

rgk
rgk

Reputation: 866

You can make use of the parseDouble function offered by the Double class if you are okay with catching an exception.

A simple code snippet that accomplishes this would be

public class AcceptDouble {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        double number1 = 0;

        while(true){
            try {
                number1 = Double.parseDouble(sc.next());
                System.out.println("Got: " + number1);
                // DO WHAT YOU WANT WITH NUMBER1
            }
            catch (NumberFormatException timeToBreak) {
                System.out.println("Time to break");
                break;
            }
        }
    }
}

This keeps accepting new input as long as a valid number is entered. A sample output snippet attached for reference

34
Got: 34.0
23
Got: 23.0
12
Got: 12.0
end
Time to break

Upvotes: 0

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 726479

First, here is how you can modify your code to make it work - you can add a boolean flag to indicate that the loop has succeeded:

double number1 = 0;
boolean success;
do {
    success = true;
    try {
        System.out.print("First number: ");
        number1 = input.nextDouble();
    } catch (InputMismatchException e) {
        System.out.print("This is not a number. ");
        success = false;
    }
    input.nextLine();
} while (!success);

Next, should rework your code to avoid catching exceptions by calling hasNextDouble before calling nextDouble:

double number1 = 0;
while(true) {
    System.out.print("First number: ");
    if (input.hasNextDouble()) {
        number1 = input.nextDouble(); // This will not throw an exception
        break;
    }
    System.out.print("This is not a number. ");
    input.nextLine();        
}

Upvotes: 1

Related Questions