使用 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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 16:05:44  来源:igfitidea点击:

Validating input using java.util.Scanner

javavalidationjava.util.scanner

提问by bhavna raghuvanshi

I'm taking user input from System.inusing 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.hasNextXXXmethods

Scanner.hasNextXXX方法概述

java.util.Scannerhas many hasNextXXXmethods that can be used to validate input. Here's a brief overview of all of them:

java.util.Scanner有许多hasNextXXX方法可用于验证输入。以下是所有这些的简要概述:

Scanneris 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 findand skipmethods that ignoresdelimiters.

Scanner由于它是基于正则表达式的,因此能够实现更多功能。一个重要的特性是useDelimiter(String pattern),它允许您定义分隔标记的模式。也有findskip方法是忽略分隔符。

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 intfrom 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/catchInteger.parseInt/NumberFormatExceptioncombo. By contract, a Scannerguaranteesthat if it hasNextInt(), then nextInt()will peacefully give you that int, and will notthrow any NumberFormatException/InputMismatchException/NoSuchElementException.

请注意Scanner.hasNextInt(),与更详细的try/catchInteger.parseInt/NumberFormatException组合相比,使用起来要容易得多。通过合同,一个Scanner保证,如果它hasNextInt(),然后nextInt()将安静地给你int,并不会引发任何NumberFormatException/ InputMismatchException/ NoSuchElementException

Related questions

相关问题



Example 2: Multiple hasNextXXXon the same token

示例 2:hasNextXXX同一令牌上的多个

Note that the snippet above contains a sc.next()statement to advance the Scanneruntil it hasNextInt(). It's important to realize that noneof thehasNextXXXmethods advance theScannerpast 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 hasNextXXXtest, then you need to advance the Scannerone way or another (e.g. next(), nextLine(), skip, etc).
  • If one hasNextXXXtest fails, you can stilltest if it perhaps hasNextYYY!
  • 如果你需要跳过“垃圾”输入失败的hasNextXXX测试,那么你需要提前Scanner一个这样或那样的(例如next()nextLine()skip等)。
  • 如果一项hasNextXXX测试失败,您仍然可以测试它是否可能hasNextYYY

Here's an example of performing multiple hasNextXXXtests.

这是执行多个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 ScannerhasNextInt(), then it also hasNextLong(), but it's not necessarily truethe other way around. More often than not you'd want to do the more specific test before the more general test.

请注意,测试的顺序很重要。如果 a ScannerhasNextInt(),那么它也是hasNextLong(),但不一定true相反。通常情况下,您希望在更一般的测试之前进行更具体的测试。



Example 3 : Validating vowels

示例 3:验证元音

Scannerhas 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

接口链接

Related questions

相关问题

References

参考



Example 4: Using two Scannerat 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 Scannertakes the nextLine()from the first Scanneras input. Here's an example:

有时您需要逐行扫描,一行中有多个标记。完成此操作的最简单方法是使用twoScanner,其中第二个ScannernextLine()第一个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

概括

  • Scannerprovides a rich set of features, such as hasNextXXXmethods for validation.
  • Proper usage of hasNextXXX/nextXXXin combination means that a Scannerwill NEVERthrow an InputMismatchException/NoSuchElementException.
  • Always remember that hasNextXXXdoes not advance the Scannerpast any input.
  • Don't be shy to create multiple Scannerif necessary. Two simple Scanneris often better than one overly complex Scanner.
  • 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 Scannermethod that takes a String patternargument is regex-based.
    • Tip: an easy way to turn any Stringinto a literal pattern is to Pattern.quoteit.
  • Scanner提供了一组丰富的功能,例如hasNextXXX验证方法。
  • 正确使用hasNextXXX/nextXXX相结合的手段,一个Scanner永远不会抛出InputMismatchException/ NoSuchElementException
  • 永远记住,hasNextXXX不要提前Scanner过去任何输入。
  • Scanner如有必要,不要害羞地创建多个。两个简单Scanner往往胜过一个过于复杂Scanner
  • 最后,即使您没有任何使用高级正则表达式功能的计划,也要记住哪些方法是基于正则表达式的,哪些不是。任何Scanner接受String pattern参数的方法都是基于正则表达式的。
    • 提示:将 anyString变成文字模式的一种简单方法是 to Pattern.quoteit。

回答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);