问题
I'm writing a program in which I need to take input from the keyboard. I need to take a number in, yet I'm not sure if it's an int
or a double
. Here's the code that I have (for that specific part):
import java.io.*;
import java.util.*;
//...
Scanner input = new Scanner(System.in);
int choice = input.nextInt();
I know I can get a String
and do parseInt()
or parseDouble()
, but I don't know which one it'll be.
回答1:
Just use a double
no matter what it is. There is no noticeable loss on using a double for integral values.
Scanner input = new Scanner(System.in);
double choice = input.nextDouble();
Then, if you need to know whether you've gotten 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 catch
ing NumberFormatException, don't search the string for a period (which might not even be correct, for example if the input is 1e-3
it's a double (0.001
) but doesn't have a period. Just parse it as a double
and move on.
Also, don't forget that both nextInt()
and nextDouble()
do not capture the newline, so you need to capture it with a nextLine() after using them.
回答2:
Well, ints are also doubles so if you assume that everything is a double you will be OK with your logic. Like this:
import java.io.*;
import java.util.*;
Scanner input = new Scanner(System.in);
double choice = input.nextDouble();
It only get complex if you needed the input to be an integer for whatever reason. And then, parseInt() to test for int would be just fine.
回答3:
What I would do is get String
input, and parse it as either a double or an 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's 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's 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's 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 just parsing a double (doubles have a different range of possible values than integers), and you can handle the cases where neither a valid integer or double was entered.
If an exception is thrown by the code inside the try
block, the code in the catch block is executed. In our case, if an exception is thrown by the parseInt()
method, we execute the code in the catch block, where the second try-block is. If an exception os thrown by the parseDouble()
method, then we execute the code inside the second catch-block, which prints an error message.
回答4:
You could try using the floor function to check if it is a double. In case you don't know, the floor function basically cuts off any decimal numbers. So you can compare the number with and without the decimal. If they are the same, then the number can be treated as an integer, otherwise a double (assuming you don't need to worry about large numbers like longs).
String choice = input.nextLine();
if (Double.parseDouble(choice) == Math.floor(Double.parseDouble(choice)) {
//choice is an int
} else {
//choice is a double
}
来源:https://stackoverflow.com/questions/31642352/how-do-i-read-input-that-could-be-an-int-or-a-double