How to read input, which can be int or double?

I am writing a program in which I need to enter keyboard input. I need to take a number, but I'm not sure if it is int or double . Here is the code I have (for this specific part):

 import java.io.*; import java.util.*; //... Scanner input = new Scanner(System.in); int choice = input.nextInt(); 

I know that I can get String and do parseInt() or parseDouble() , but I don't know which one it will be.

+5
source share
4 answers

Just use double no matter what it is. There is no noticeable loss when using double for integral values.

 Scanner input = new Scanner(System.in); double choice = input.nextDouble(); 

Then, if you need to know if you have a double or not, you can check it using Math.floor :

 if (choice == Math.floor(choice)) { int choiceInt = (int) choice); // treat it as an int } 

Don’t mess with NumberFormatException , don’t look for a string for a period (which may not even be true, for example, if input 1e-3 is double ( 0.001 ) but has no period. Just parse it as double and go.

Also, don't forget that both nextInt() and nextDouble() do not capture a new line, so you need to write it with nextLine() after using them.

+2
source

Well, ints are also doubled, so if you assume that everything is doubled, you'll be fine with your logic. Like this:

 import java.io.*; import java.util.*; Scanner input = new Scanner(System.in); double choice = input.nextDouble(); 

It becomes complicated if you need input to be an integer for any reason. And then parseInt () to check for int would be just fine.

+5
source

You can try using the floor function to check if it is double. If you don't know, the gender function basically disables any decimal numbers. Thus, you can compare a number with a decimal point and without it. If they are the same, then the number can be thought of as an integer, otherwise a double (assuming you don't need to worry about large numbers such as longs).

 String choice = input.nextLine(); if (Double.parseDouble(choice) == Math.floor(Double.parseDouble(choice)) { //choice is an int } else { //choice is a double } 
0
source

What I would do is get the String input and parse it as a double or integer.

 String str = input.next(); int i = 0; double d = 0d; boolean isInt = false, isDouble = false; try { // If the below method call doesn't throw an exception, we know that it a valid integer i = Integer.parseInt(str); isInt = true }catch(NumberFormatException e){ try { // It wasn't in the right format for an integer, so let try parsing it as a double d = Double.parseDouble(str); isDouble = true; }catch(NumberFormatException e){ // An error was thrown when parsing it as a double, so it neither an int or double System.out.println(str + " is neither an int or a double"); } } // isInt and isDouble now store whether or not the input was an int or a double // Both will be false if it wasn't a valid int or double 

This way you can ensure that you don't lose integer precision by simply parsing the double (doubles a different range of possible values ​​than integers), and you can handle cases where neither a real integer nor a double has been entered.

If an exception is thrown by code inside the try block, the code in the catch block is executed. In our case, if the exception is thrown by the parseInt() method, we execute the code in the catch block, where the second try block is located. If the exception is thrown by the parseDouble() method, then we execute the code inside the second catch block, which displays an error message.

0
source

All Articles