使用 java.util.Scanner 验证输入
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3059333/
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
Validating input using java.util.Scanner
提问by bhavna raghuvanshi
I'm taking user input from System.in
using a java.util.Scanner
. I need to validate the input for things like:
我正在从System.in
使用java.util.Scanner
. 我需要验证以下内容的输入:
- It must be a non-negative number
- It must be an alphabetical letter
- ... etc
- 它必须是一个非负数
- 它必须是一个字母
- ... 等等
What's the best way to do this?
做到这一点的最佳方法是什么?
回答by polygenelubricants
Overview of Scanner.hasNextXXX
methods
Scanner.hasNextXXX
方法概述
java.util.Scanner
has many hasNextXXX
methods that can be used to validate input. Here's a brief overview of all of them:
java.util.Scanner
有许多hasNextXXX
方法可用于验证输入。以下是所有这些的简要概述:
hasNext()
- does it have anytoken at all?hasNextLine()
- does it have another line of input?- For Java primitives
hasNextInt()
- does it have a token that can be parsed into anint
?- Also available are
hasNextDouble()
,hasNextFloat()
,hasNextByte()
,hasNextShort()
,hasNextLong()
, andhasNextBoolean()
- As bonus, there's also
hasNextBigInteger()
andhasNextBigDecimal()
- The integral types also has overloads to specify radix (for e.g. hexadecimal)
- Regular expression-based
hasNext()
- 它有任何令牌吗?hasNextLine()
- 它有另一行输入吗?- 对于 Java 原语
hasNextInt()
- 它有一个可以解析为 的令牌int
吗?- 此外,还包括
hasNextDouble()
,hasNextFloat()
,hasNextByte()
,hasNextShort()
,hasNextLong()
,和hasNextBoolean()
- 作为奖励,还有
hasNextBigInteger()
和hasNextBigDecimal()
- 整数类型也有重载来指定基数(例如十六进制)
- 基于正则表达式
Scanner
is capable of more, enabled by the fact that it's regex-based. One important feature is useDelimiter(String pattern)
, which lets you define what patternseparates your tokens. There are also find
and skip
methods that ignoresdelimiters.
Scanner
由于它是基于正则表达式的,因此能够实现更多功能。一个重要的特性是useDelimiter(String pattern)
,它允许您定义分隔标记的模式。也有find
和skip
方法是忽略分隔符。
The following discussion will keep the regex as simple as possible, so the focus remains on Scanner
.
以下讨论将使正则表达式尽可能简单,因此重点仍然放在Scanner
.
Example 1: Validating positive ints
示例 1:验证正整数
Here's a simple example of using hasNextInt()
to validate positive int
from the input.
这是一个简单的示例,hasNextInt()
用于验证int
输入中的正数。
Scanner sc = new Scanner(System.in);
int number;
do {
System.out.println("Please enter a positive number!");
while (!sc.hasNextInt()) {
System.out.println("That's not a number!");
sc.next(); // this is important!
}
number = sc.nextInt();
} while (number <= 0);
System.out.println("Thank you! Got " + number);
Here's an example session:
这是一个示例会话:
Please enter a positive number!
five
That's not a number!
-3
Please enter a positive number!
5
Thank you! Got 5
请输入正数!
五
那不是数字!
-3
请输入正数!
5
谢谢!得了 5
Note how much easier Scanner.hasNextInt()
is to use compared to the more verbose try/catch
Integer.parseInt
/NumberFormatException
combo. By contract, a Scanner
guaranteesthat if it hasNextInt()
, then nextInt()
will peacefully give you that int
, and will notthrow any NumberFormatException
/InputMismatchException
/NoSuchElementException
.
请注意Scanner.hasNextInt()
,与更详细的try/catch
Integer.parseInt
/NumberFormatException
组合相比,使用起来要容易得多。通过合同,一个Scanner
保证,如果它hasNextInt()
,然后nextInt()
将安静地给你int
,并不会引发任何NumberFormatException
/ InputMismatchException
/ NoSuchElementException
。
Related questions
相关问题
- How to use Scanner to accept only valid int as input
- How do I keep a scanner from throwing exceptions when the wrong type is entered? (java)
Example 2: Multiple hasNextXXX
on the same token
示例 2:hasNextXXX
同一令牌上的多个
Note that the snippet above contains a sc.next()
statement to advance the Scanner
until it hasNextInt()
. It's important to realize that noneof thehasNextXXX
methods advance theScanner
past any input!You will find that if you omit this line from the snippet, then it'd go into an infinite loop on an invalid input!
请注意,上面的代码段包含一个sc.next()
语句来推进Scanner
直到它hasNextInt()
。认识到这一点很重要没有的hasNextXXX
方法推进Scanner
执行任何输入!你会发现,如果你从代码片段中省略这一行,那么它会在无效输入上进入无限循环!
This has two consequences:
这有两个后果:
- If you need to skip the "garbage" input that fails your
hasNextXXX
test, then you need to advance theScanner
one way or another (e.g.next()
,nextLine()
,skip
, etc). - If one
hasNextXXX
test fails, you can stilltest if it perhapshasNextYYY
!
- 如果你需要跳过“垃圾”输入失败的
hasNextXXX
测试,那么你需要提前Scanner
一个这样或那样的(例如next()
,nextLine()
,skip
等)。 - 如果一项
hasNextXXX
测试失败,您仍然可以测试它是否可能hasNextYYY
!
Here's an example of performing multiple hasNextXXX
tests.
这是执行多个hasNextXXX
测试的示例。
Scanner sc = new Scanner(System.in);
while (!sc.hasNext("exit")) {
System.out.println(
sc.hasNextInt() ? "(int) " + sc.nextInt() :
sc.hasNextLong() ? "(long) " + sc.nextLong() :
sc.hasNextDouble() ? "(double) " + sc.nextDouble() :
sc.hasNextBoolean() ? "(boolean) " + sc.nextBoolean() :
"(String) " + sc.next()
);
}
Here's an example session:
这是一个示例会话:
5
(int) 5
false
(boolean) false
blah
(String) blah
1.1
(double) 1.1
100000000000
(long) 100000000000
exit
5
(int) 5
false
(boolean) false
blah
(String) blah
1.1
(double) 1.1
100000000000
(long) 100000000000
exit
Note that the order of the tests matters. If a Scanner
hasNextInt()
, then it also hasNextLong()
, but it's not necessarily true
the other way around. More often than not you'd want to do the more specific test before the more general test.
请注意,测试的顺序很重要。如果 a Scanner
hasNextInt()
,那么它也是hasNextLong()
,但不一定true
相反。通常情况下,您希望在更一般的测试之前进行更具体的测试。
Example 3 : Validating vowels
示例 3:验证元音
Scanner
has many advanced features supported by regular expressions. Here's an example of using it to validate vowels.
Scanner
具有许多正则表达式支持的高级功能。这是一个使用它来验证元音的示例。
Scanner sc = new Scanner(System.in);
System.out.println("Please enter a vowel, lowercase!");
while (!sc.hasNext("[aeiou]")) {
System.out.println("That's not a vowel!");
sc.next();
}
String vowel = sc.next();
System.out.println("Thank you! Got " + vowel);
Here's an example session:
这是一个示例会话:
Please enter a vowel, lowercase!
5
That's not a vowel!
z
That's not a vowel!
e
Thank you! Got e
请输入元音,小写!
5
那不是元音!
z
那不是元音!
e
谢谢!得到了
In regex, as a Java string literal, the pattern "[aeiou]"
is what is called a "character class"; it matches any of the letters a
, e
, i
, o
, u
. Note that it's trivial to make the above test case-insensitive: just provide such regex pattern to the Scanner
.
在正则表达式中,作为 Java 字符串文字,模式"[aeiou]"
就是所谓的“字符类”;它匹配任何字母a
, e
, i
, o
, u
。请注意,使上述测试不区分大小写是微不足道的:只需将这样的正则表达式模式提供给Scanner
.
API links
接口链接
hasNext(String pattern)
- Returnstrue
if the next token matches the pattern constructed from the specified string.java.util.regex.Pattern
hasNext(String pattern)
- 返回true
下一个标记是否与从指定字符串构造的模式匹配。java.util.regex.Pattern
Related questions
相关问题
References
参考
Example 4: Using two Scanner
at once
实施例4:使用两个Scanner
在一次
Sometimes you need to scan line-by-line, with multiple tokens on a line. The easiest way to accomplish this is to use twoScanner
, where the second Scanner
takes the nextLine()
from the first Scanner
as input. Here's an example:
有时您需要逐行扫描,一行中有多个标记。完成此操作的最简单方法是使用twoScanner
,其中第二个Scanner
将nextLine()
第一个Scanner
作为输入。下面是一个例子:
Scanner sc = new Scanner(System.in);
System.out.println("Give me a bunch of numbers in a line (or 'exit')");
while (!sc.hasNext("exit")) {
Scanner lineSc = new Scanner(sc.nextLine());
int sum = 0;
while (lineSc.hasNextInt()) {
sum += lineSc.nextInt();
}
System.out.println("Sum is " + sum);
}
Here's an example session:
这是一个示例会话:
Give me a bunch of numbers in a line (or 'exit')
3 4 5
Sum is 12
10 100 a million dollar
Sum is 110
wait what?
Sum is 0
exit
在一行中给我一堆数字(或“退出”)
3 4 5
总和是 12
10 100 一百万美元
总和是 110
等等什么?
总和为 0
退出
In addition to Scanner(String)
constructor, there's also Scanner(java.io.File)
among others.
除了Scanner(String)
构造函数,还有Scanner(java.io.File)
其他的。
Summary
概括
Scanner
provides a rich set of features, such ashasNextXXX
methods for validation.- Proper usage of
hasNextXXX/nextXXX
in combination means that aScanner
will NEVERthrow anInputMismatchException
/NoSuchElementException
. - Always remember that
hasNextXXX
does not advance theScanner
past any input. - Don't be shy to create multiple
Scanner
if necessary. Two simpleScanner
is often better than one overly complexScanner
. - Finally, even if you don't have any plans to use the advanced regex features, do keep in mind which methods are regex-based and which aren't. Any
Scanner
method that takes aString pattern
argument is regex-based.- Tip: an easy way to turn any
String
into a literal pattern is toPattern.quote
it.
- Tip: an easy way to turn any
Scanner
提供了一组丰富的功能,例如hasNextXXX
验证方法。- 正确使用
hasNextXXX/nextXXX
相结合的手段,一个Scanner
将永远不会抛出InputMismatchException
/NoSuchElementException
。 - 永远记住,
hasNextXXX
不要提前Scanner
过去任何输入。 Scanner
如有必要,不要害羞地创建多个。两个简单Scanner
往往胜过一个过于复杂Scanner
。- 最后,即使您没有任何使用高级正则表达式功能的计划,也要记住哪些方法是基于正则表达式的,哪些不是。任何
Scanner
接受String pattern
参数的方法都是基于正则表达式的。- 提示:将 any
String
变成文字模式的一种简单方法是 toPattern.quote
it。
- 提示:将 any
回答by mahju
If you are parsing string data from the console or similar, the best way is to use regular expressions. Read more on that here: http://java.sun.com/developer/technicalArticles/releases/1.4regex/
如果您从控制台或类似工具解析字符串数据,最好的方法是使用正则表达式。在此处阅读更多相关信息:http: //java.sun.com/developer/technicalArticles/releases/1.4regex/
Otherwise, to parse an int from a string, try Integer.parseInt(string). If the string is not a number, you will get an exception. Otherise you can then perform your checks on that value to make sure it is not negative.
否则,要从字符串解析 int,请尝试 Integer.parseInt(string)。如果字符串不是数字,则会出现异常。否则,您可以对该值执行检查以确保它不是负数。
String input;
int number;
try
{
number = Integer.parseInt(input);
if(number > 0)
{
System.out.println("You positive number is " + number);
}
} catch (NumberFormatException ex)
{
System.out.println("That is not a positive number!");
}
To get a character-only string, you would probably be better of looping over each character checking for digits, using for instance Character.isLetter(char).
要获得仅字符的字符串,您可能最好循环检查每个字符是否有数字,例如使用Character.isLetter(char)。
String input
for(int i = 0; i<input.length(); i++)
{
if(!Character.isLetter(input.charAt(i)))
{
System.out.println("This string does not contain only letters!");
break;
}
}
Good luck!
祝你好运!
回答by Romain Linsolas
One idea:
一个想法:
try {
int i = Integer.parseInt(myString);
if (i < 0) {
// Error, negative input
}
} catch (NumberFormatException e) {
// Error, not a number.
}
There is also, in commons-langlibrary the CharUtilsclass that provides the methods isAsciiNumeric()
to check that a character is a number, and isAsciiAlpha()
to check that the character is a letter...
在commons-lang库中还有CharUtils类,它提供了isAsciiNumeric()
检查字符是否为数字以及isAsciiAlpha()
检查字符是否为字母的方法......
回答by Duane
For checking Strings for letters you can use regular expressions for example:
要检查字符串中的字母,您可以使用正则表达式,例如:
someString.matches("[A-F]");
For checking numbers and stopping the program crashing, I have a quite simple class you can find below where you can define the range of values you want. Here
为了检查数字和阻止程序崩溃,我有一个非常简单的类,您可以在下面找到您可以在其中定义所需值的范围。 这里
public int readInt(String prompt, int min, int max)
{
Scanner scan = new Scanner(System.in);
int number = 0;
//Run once and loop until the input is within the specified range.
do
{
//Print users message.
System.out.printf("\n%s > ", prompt);
//Prevent string input crashing the program.
while (!scan.hasNextInt())
{
System.out.printf("Input doesn't match specifications. Try again.");
System.out.printf("\n%s > ", prompt);
scan.next();
}
//Set the number.
number = scan.nextInt();
//If the number is outside range print an error message.
if (number < min || number > max)
System.out.printf("Input doesn't match specifications. Try again.");
} while (number < min || number > max);
return number;
}
回答by Dylan Sharhon
Here's a minimalist way to do it.
这是一种极简主义的方法。
System.out.print("Please enter an integer: ");
while(!scan.hasNextInt()) scan.next();
int demoInt = scan.nextInt();
回答by Aman Vikram Singh
what i have tried is that first i took the integer input and checked that whether its is negative or not if its negative then again take the input
我尝试过的是,首先我接受了整数输入并检查它是否为负,如果它为负,然后再次接受输入
Scanner s=new Scanner(System.in);
int a=s.nextInt();
while(a<0)
{
System.out.println("please provide non negative integer input ");
a=s.nextInt();
}
System.out.println("the non negative integer input is "+a);
Here, you need to take the character input first and check whether user gave character or not if not than again take the character input
在这里,您需要先输入字符,然后检查用户是否输入了字符,如果没有,则再次输入字符
char ch = s.findInLine(".").charAt(0);
while(!Charcter.isLetter(ch))
{
System.out.println("please provide a character input ");
ch=s.findInLine(".").charAt(0);
}
System.out.println("the character input is "+ch);