3

below is the code:

    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");
             //enter a double again
        }else {
            System.out.println("Input is Double");
            //break
        }
    } catch(Exception e) {
        if(input.toCharArray().length == 1) {
            System.out.println("Input is Character");
             //enter a double again
        }else {
            System.out.println("Input is String");
            //enter a double again
        }
    }

taken from here: how to check the data type validity of user's input (Java Scanner class)

however, when i input 1.0 or 0.0, it is still considered as an integer, is 1.0 not considered a double?

Please help guys, thank you!

3
  • 2
    i suggest scan the input as a string and than try to parse it as an int, if it is successfull, it is an int. if not, then try to parse it as a float Commented Apr 16, 2014 at 10:28
  • before parsing you can check if it has character dot (.) to decide if its double or integer Commented Apr 16, 2014 at 10:29
  • @maxx777 input is a string parse to a double... Commented Apr 16, 2014 at 10:33

6 Answers 6

3

If you want to treat 1.0 as a Double an 1 as an Integer, you need to work with the input variable, which is of type String.

Java will always treat Double x = 1 in the same way as Double y = 1.0 (meaning 1 is a valid Double), so you will not be able to distinguish them with code.

Since you have the original string representation of the input, use a regex or some other validation to check it. For instance a sample regex pattern for double would look like "[0-9]+(\.){0,1}[0-9]*" and for an integer "[0-9]+" or "\d+"

Here is an example:

final static String DOUBLE_PATTERN = "[0-9]+(\.){0,1}[0-9]*";
final static String INTEGER_PATTERN = "\d+";

Scanner scan = new Scanner(System.in);
String input = scan.next();

if (Pattern.matches(INTEGER_PATTERN, input)) {
    System.out.println("Input is Integer");
    //enter a double again
} else if (Pattern.matches(DOUBLE_PATTERN, input)) {
    System.out.println("Input is Double");
    //break
} else {
    System.out.println("Input is not a number");
    if (input.length == 1) {
        System.out.println("Input is a Character");
        //enter a double again
    } else {
        System.out.println("Input is a String");
        //enter a double again
    }
}
Sign up to request clarification or add additional context in comments.

Comments

2

1.0 is typically considered a valid double, but the code has been written explicitly to threat whole numbers as invalid doubles(printing that they are intergers). To change this behavior remove the inner if:

try{
    double isNum = Double.parseDouble(input);
    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");
    }
}

4 Comments

let me see if its throw an error, i think it will accept integer as valid because string 1 can be parse as 1.0?
1 is a valid double too. I would expect the code above to succeed for similar input.
if 1 is a valid double then my question is answered. thank you.
@user3540710, if the answer works for you, you should mark it as correct via the green tick sign on the left
0

Check, if the string you are validating contains a dot - then, if parseDouble succeeds, it could be treated as a double value (even for 1.0).

Comments

0

You check if 1.0 is equal to 1, which mathematically is correct. Therefore your first if will return true, and it will be considered an int. It seems that the whole idea with the program is to check doubles and see if they have decimals or not.

Comments

0
Math.floor(x)

returns a double, and since 1.0 is indeed the same as 1.0, your program will enter the first if-block.

Comments

0

I think your test is flawed. You say "0.0 and 1.0 are parsed as integers" based on the condition

isNum == Math.floor(isNum)

Double-precision floating-point numbers are not a random garbage of bits: they express a precise quantity, and it happens that the numbers zero and one can be represented exactly (see the bit patterns) and thus they are equal to Math.floor(val) (btw, this happens for lots of integer values, not only zero and one)

So you should reconsider your test before your code. Consider that likely a user will never input an integer or a double: what she types is a decimal number, and you should deal with the input depending on what it's used for (the choice is among String, BigDecimal, Number, Integer, Double and so on...)

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.