使用 java.util.Scanner 验证输入 [英] Validating input using java.util.Scanner
问题描述
我使用 java.util.Scanner
从 System.in
获取用户输入.我需要验证以下内容的输入:
- 必须是非负数
- 它必须是一个字母
- ...等
最好的方法是什么?
Scanner.hasNextXXX
方法概述
java.util.Scanner
有许多 hasNextXXX
方法可用于验证输入.以下是所有这些的简要概述:
hasNext()
- 它有任何令牌吗?hasNextLine()
- 它还有另一行输入吗?- 对于 Java 原语
hasNextInt()
- 是否有可以解析为int
的标记?- 还有 <代码>hasNextDouble(),
hasNextFloat()
,hasNextByte()
,hasNextShort()
,hasNextLong()
和hasNextBoolean()
- 作为奖励,还有
hasNextBigInteger()
和hasNextBigDecimal()
- 整数类型也有重载来指定基数(例如十六进制)
- 基于正则表达式
Scanner
具有更多功能,因为它基于正则表达式.一项重要功能是 useDelimiter(String pattern)
,它让你定义什么模式分隔你的令牌.还有 find
和 skip
方法忽略分隔符.
下面的讨论将尽可能简化正则表达式,所以重点仍然放在 Scanner
上.
示例 1:验证正整数
这是一个使用 hasNextInt()
从输入中验证正 int
的简单示例.
Scanner sc = new Scanner(System.in);整数;做 {System.out.println("请输入正数!");而 (!sc.hasNextInt()) {System.out.println("这不是数字!");sc.next();//这个很重要!}数字 = sc.nextInt();} while (number <= 0);System.out.println("谢谢!得到" + number);
这是一个示例会话:
<块引用>请输入正数!
五
这不是数字!
-3
请输入一个正数!
5
谢谢!得到 5
注意 请注意,上面的代码段包含一个 这有两个后果: 这是执行多个 这是一个示例会话: 5 请注意,测试的顺序很重要.如果一个 这是一个示例会话: 请输入元音,小写! 在正则表达式中,作为Java字符串文字,模式 有时您需要逐行扫描,一行中有多个标记.完成此操作的最简单方法是使用 two 这是一个示例会话: 在一行中给我一堆数字(或退出") 除了 I'm taking user input from What's the best way to do this? The following discussion will keep the regex as simple as possible, so the focus remains on Here's a simple example of using Here's an example session: Please enter a positive number! Note how much easier Note that the snippet above contains a This has two consequences: Here's an example of performing multiple Here's an example session: 5 Note that the order of the tests matters. If a Here's an example session: Please enter a vowel, lowercase! In regex, as a Java string literal, the pattern Sometimes you need to scan line-by-line, with multiple tokens on a line. The easiest way to accomplish this is to use two Here's an example session: Give me a bunch of numbers in a line (or 'exit') In addition to 这篇关于使用 java.util.Scanner 验证输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!Scanner.hasNextInt()
与更冗长的 try/catch
Integer.parseInt
/NumberFormatException
组合.根据合同,Scanner
保证如果它hasNextInt()
,那么nextInt()
会和平地给你int
,并且不会抛出任何NumberFormatException
/InputMismatchException
/NoSuchElementException
.>相关问题
<小时>示例2:同一个token上有多个
hasNextXXX
sc.next()
语句,用于推进 Scanner
直到它 hasNextInt()
.重要的是要认识到none hasNextXXX
方法推进 Scanner
跳过任何输入!你会发现如果你从代码片段中省略这一行,那么它会在无效输入上进入无限循环!hasNextXXX
测试失败的垃圾"输入,那么您需要以一种或另一种方式推进 Scanner
(例如 next()
、nextLine()
、skip
等).hasNextXXX
测试失败,你可以仍然测试它是否可能hasNextYYY
!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() :(字符串)" + sc.next());}
(int) 5
假
(布尔值)假
等等
(字符串)等等
1.1
(双)1.1
100000000000
(长)100000000000
退出Scanner
hasNextInt()
,那么它也是 hasNextLong()
,但它不一定 true
反之大约.通常情况下,您希望在进行更一般的测试之前先进行更具体的测试.示例 3:验证元音
Scanner
具有许多正则表达式支持的高级功能.这是一个使用它来验证元音的示例.Scanner sc = new Scanner(System.in);System.out.println("请输入元音,小写!");而 (!sc.hasNext("[aeiou]")) {System.out.println("那不是元音!");sc.next();}字符串元音 = sc.next();System.out.println("谢谢!得到" + 元音);
5
那不是元音!
z
那不是元音!
电子
谢谢!得到了"[aeiou]"
就是所谓的字符类";它匹配任何字母 a
、e
、i
、o
、u
.请注意,使上述测试不区分大小写是微不足道的:只需向 Scanner
提供这样的正则表达式模式即可.API 链接
hasNext(String pattern)
- 如果下一个标记与从指定字符串构造的模式匹配,则返回 true
.java.util.regex.Pattern
相关问题
参考资料
<小时>示例 4:同时使用两个
Scanner
Scanner
,其中第二个 Scanner
从第一个 Scanner
作为输入.举个例子:Scanner sc = new Scanner(System.in);System.out.println("给我一行一行的数字(或'退出')");while (!sc.hasNext("exit")) {Scanner lineSc = new Scanner(sc.nextLine());整数总和 = 0;而 (lineSc.hasNextInt()) {sum += lineSc.nextInt();}System.out.println("总和为" + sum);}
3 4 5
总和是 12
10 100 一百万美元
总和是 110
等什么?
总和为 0
退出Scanner(String)
构造函数,还有 Scanner(java.io.File)
等.总结
Scanner
提供了一组丰富的功能,例如用于验证的 hasNextXXX
方法.hasNextXXX/nextXXX
意味着 Scanner
将 NEVER 抛出 InputMismatchException
/<代码>NoSuchElementException.hasNextXXX
不会让 Scanner
超过任何输入.Scanner
.两个简单的 Scanner
通常比一个过于复杂的 Scanner
要好.String pattern
参数的 Scanner
方法都是基于正则表达式的.String
转换为文字模式的一种简单方法是 Pattern.quote
它.System.in
using a java.util.Scanner
. I need to validate the input for things like:
Overview of
Scanner.hasNextXXX
methodsjava.util.Scanner
has many hasNextXXX
methods that can be used to validate input. Here's a brief overview of all of them:
hasNext()
- does it have any token at all?hasNextLine()
- does it have another line of input?
hasNextInt()
- does it have a token that can be parsed into an int
?hasNextDouble()
, hasNextFloat()
, hasNextByte()
, hasNextShort()
, hasNextLong()
, and hasNextBoolean()
hasNextBigInteger()
and hasNextBigDecimal()
hasNext(String pattern)
hasNext(Pattern pattern)
is the Pattern.compile
overloadScanner
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 pattern separates your tokens. There are also find
and skip
methods that ignores delimiters.Scanner
.
Example 1: Validating positive ints
hasNextInt()
to validate positive int
from the input.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);
five
That's not a number!
-3
Please enter a positive number!
5
Thank you! Got 5Scanner.hasNextInt()
is to use compared to the more verbose try/catch
Integer.parseInt
/NumberFormatException
combo. By contract, a Scanner
guarantees that if it hasNextInt()
, then nextInt()
will peacefully give you that int
, and will not throw any NumberFormatException
/InputMismatchException
/NoSuchElementException
.Related questions
Example 2: Multiple
hasNextXXX
on the same tokensc.next()
statement to advance the Scanner
until it hasNextInt()
. It's important to realize that none of the hasNextXXX
methods advance the Scanner
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!
hasNextXXX
test, then you need to advance the Scanner
one way or another (e.g. next()
, nextLine()
, skip
, etc).hasNextXXX
test fails, you can still test if it perhaps hasNextYYY
!hasNextXXX
tests.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()
);
}
(int) 5
false
(boolean) false
blah
(String) blah
1.1
(double) 1.1
100000000000
(long) 100000000000
exit 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.
Example 3 : Validating vowels
Scanner
has many advanced features supported by regular expressions. Here's an example of using it to validate vowels.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);
5
That's not a vowel!
z
That's not a vowel!
e
Thank you! Got e"[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
.API links
hasNext(String pattern)
- Returns true
if the next token matches the pattern constructed from the specified string.java.util.regex.Pattern
Related questions
References
Example 4: Using two
Scanner
at onceScanner
, where the second Scanner
takes the nextLine()
from the first Scanner
as input. Here's an example: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);
}
3 4 5
Sum is 12
10 100 a million dollar
Sum is 110
wait what?
Sum is 0
exitScanner(String)
constructor, there's also Scanner(java.io.File)
among others.
Summary
Scanner
provides a rich set of features, such as hasNextXXX
methods for validation.hasNextXXX/nextXXX
in combination means that a Scanner
will NEVER throw an InputMismatchException
/NoSuchElementException
.hasNextXXX
does not advance the Scanner
past any input.Scanner
if necessary. Two simple Scanner
is often better than one overly complex Scanner
.Scanner
method that takes a String pattern
argument is regex-based.
String
into a literal pattern is to Pattern.quote
it.