3

I am trying to make a simple UI that asks users to input double-type numbers, if theirs input is not of double type, the program should keep printing the prompt until user inputs a valid double type. My code below is not quite working yet, because when a user types in a valid double type, the program does not do anything unless the user types another double type number. I guess the condition (sc.hasNextDouble()) in the while loop consumes the first valid input. How to correct this? thanks a lot

Scanner sc = new Scanner(System.in);

System.out.println("Type a double-type number:");
while (!sc.hasNextDouble())
{
    System.out.println("Invalid input\n Type the double-type number:");
    sc.next();
}
userInput = sc.nextDouble();    // need to check the data type?
0

5 Answers 5

6

Since you may not get a double entered, best to read in a String, then attempt to convert it to a double. The standard pattern is:

Scanner sc = new Scanner(System.in);
double userInput = 0;
while (true) {
    System.out.println("Type a double-type number:");
    try {
        userInput = Double.parseDouble(sc.next());
        break; // will only get to here if input was a double
    } catch (NumberFormatException ignore) {
        System.out.println("Invalid input");
    }
}

The loop can't exit until a double has been entered, after which userInput will hold that value.

Note also how by putting the prompt inside the loop, you can avoid code duplication on invalid input.

Sign up to request clarification or add additional context in comments.

Comments

3

Your code works perfect: http://ideone.com/NN42UG and http://ideone.com/MVbjMz

Scanner sc = new Scanner(System.in);
System.out.println("Type a double-type number:");
while (!sc.hasNextDouble())
{
    System.out.println("Invalid input\n Type the double-type number:");
    sc.next();
}
double userInput = sc.nextDouble();    // need to check the data type?
System.out.println("Here it is: " + userInput);

For this input:

test test
int
49,5
23.4

Gives:

Type a double-type number:
Invalid input
 Type the double-type number:
Invalid input
 Type the double-type number:
Invalid input
 Type the double-type number:
Invalid input
 Type the double-type number:
Here it is: 23.4

Which is correct, since 49,5 is not a decimal number because it uses the wrong separator.

Comments

1

The way I would do it, for int vs. double would be to round and check if its still the same..

double input = sc.nextdouble();
if(input == Math.floor(input) {
    //Double
} else {
    //Int
}

Here is a way to check if the input is an Int, Double, String, or Character

import java.util.Scanner;


public class Variables {

    /**
     * @param args
     */
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        String input = scan.next();
        try{
            double isNum = Double.parseDouble(input);
            if(isNum == Math.floor(isNum)) {
                System.out.println("Input is Integer");
            }else {
                System.out.println("Input is Double");
            }
        } catch(Exception e) {
            if(input.toCharArray().length == 1) {
                System.out.println("Input is Character");
            }else {
                System.out.println("Input is String");
            }
        }

    }

}

Comments

0

What about Double.parseDouble(stringInput); when you scan the input as a String you can then parse it to see if it is a double. But, if you wrap this static method call in a try-catch statement, then you can handle the situation where a double value is not parsed.

Comments

0

I think the reason your code is not working due to first it will check the given input is of type double or not(sc.hasNextDouble()) if not then take again input(sc.hasNext()... no use of this line),then again you are taking input (userInput = sc.nextDouble())

I would suggest to do like this:

Scanner sc = new Scanner(System.in);
System.out.println("Type a double-type number:");
double userinput;
while (!sc.hasNextDouble())
{
    System.out.println("Invalid input\n Type the double-type number:");
}

userInput = sc.nextDouble(); 

you need to give the input again if you are providing double input at first time,i think if any time you give double input then you have to provide it again ,it looks to impossible to provide input only one time.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.