在Java中如何检查输入是否为整数?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19925047/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to check the input is an integer or not in Java?
提问by Belal Khan
In my program I want an integer input by the user. I want an error message to be show when user inputs a value which is not an integer. How can I do this. My program is to find area of circle. In which user will input the value of radius. But if user inputs a character I want a message to be shown saying Invalid Input.
在我的程序中,我希望用户输入一个整数。我希望当用户输入一个不是整数的值时显示错误消息。我怎样才能做到这一点。我的程序是找到圆的面积。用户将在其中输入半径值。但是如果用户输入一个字符,我希望显示一条消息,说无效输入。
This is my code:
这是我的代码:
int radius, area;
Scanner input=new Scanner(System.in);
System.out.println("Enter the radius:\t");
radius=input.nextInt();
area=3.14*radius*radius;
System.out.println("Area of circle:\t"+area);
采纳答案by BackSlash
If you are getting the user input with Scanner
, you can do:
如果您使用 获取用户输入Scanner
,则可以执行以下操作:
if(yourScanner.hasNextInt()) {
yourNumber = yourScanner.nextInt();
}
If you are not, you'll have to convert it to int
and catch a NumberFormatException
:
如果不是,则必须将其转换为int
并捕获NumberFormatException
:
try{
yourNumber = Integer.parseInt(yourInput);
}catch (NumberFormatException ex) {
//handle exception here
}
回答by Dark Knight
Using Integer.parseIn(String), you can parse string value into integer. Also you need to catch exception in case if input string is not a proper number.
使用 Integer.parseIn(String),您可以将字符串值解析为整数。如果输入字符串不是正确的数字,您还需要捕获异常。
int x = 0;
try {
x = Integer.parseInt("100"); // Parse string into number
} catch (NumberFormatException e) {
e.printStackTrace();
}
回答by Juned Ahsan
If the user input is a String
then you can try to parse it as an integer using parseInt
method, which throws NumberFormatException
when the input is not a valid number string:
如果用户输入是 aString
那么您可以尝试使用parseInt
方法将其解析为整数,该方法NumberFormatException
在输入不是有效数字字符串时抛出:
try {
int intValue = Integer.parseInt(stringUserInput));
}(NumberFormatException e) {
System.out.println("Input is not a valid integer");
}
回答by Kamlesh Arya
You can use try-catch block to check for integer value
您可以使用 try-catch 块来检查整数值
for eg:
例如:
User inputs in form of string
用户以字符串形式输入
try
{
int num=Integer.parseInt("Some String Input");
}
catch(NumberFormatException e)
{
//If number is not integer,you wil get exception and exception message will be printed
System.out.println(e.getMessage());
}
回答by Ruchira Gayan Ranaweera
You can try this way
你可以试试这个方法
String input = "";
try {
int x = Integer.parseInt(input);
// You can use this method to convert String to int, But if input
//is not an int value then this will throws NumberFormatException.
System.out.println("Valid input");
}catch(NumberFormatException e) {
System.out.println("input is not an int value");
// Here catch NumberFormatException
// So input is not a int.
}
回答by Rakesh KR
String input = "";
int inputInteger = 0;
BufferedReader br = new BufferedReader(new InputStreamReader (System.in));
System.out.println("Enter the radious: ");
try {
input = br.readLine();
inputInteger = Integer.parseInt(input);
} catch (NumberFormatException e) {
System.out.println("Please Enter An Integer");
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
float area = (float) (3.14*inputInteger*inputInteger);
System.out.println("Area = "+area);