-2

i need to check whether the number input is in decimal format or in floating point format in java coding.

in simple terms how would this check be possible?

4

2 Answers 2

0
Scanner scanner  = new Scanner(System.in);
if(scanner.hasNextDouble())
     //double stuff here
else if (scanner.hasNextFloat())
     //Float stuff here
2
  • so how would i write this out in terms of explaining the code?
    – RS786
    Aug 23, 2016 at 13:04
  • What do you mean by 'explaining the code'? The sample code I provided is pretty self-explanatory I believe. Aug 23, 2016 at 17:25
0

Here is a small method I quickly whipped up that you may find interesting....

public static boolean isFloatingPoint(Object number) {
    String type = number.getClass().getSimpleName().toUpperCase();
    return type.equals("FLOAT") || type.equals("DOUBLE");
}

You can use the same concept to determine any Object passed to your method, perhaps even:

public static boolean isJButton(Object component) {
    String type = component.getClass().getSimpleName().toUpperCase();
    return type.equals("JBUTTON");
}

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.