使用扫描仪对同一行中的多个条目进行Java输入验证

时间:2018-12-21 14:30:26

标签: java validation

我正在使用扫描仪从用户那里获取整数输入。在这种情况下,整数应介于5和10之间(含5和10)。

我使用此代码:

import java.util.Scanner;

public class valin {

    public static void main(String[] args) {

        int inputNumber;

        Scanner in = new Scanner(System.in);

        do {
            System.out.println("Enter a number between 5 and 10: ");

            while (!in.hasNextInt()) {
                System.out.println("That is not a number. Please try again: ");
                in.next();
            }

            inputNumber = in.nextInt();

            if (inputNumber < 5 || inputNumber > 10) {
                System.out.println("Needs to be a number between 5 and 10. Try again.");
            }

        } while (inputNumber < 5 || inputNumber > 10);

        System.out.println("You entered: " + inputNumber);
        in.close();

    }

}

我遇到的问题是,当用户在一行上输入多个输入时。在这种情况下,我的代码将显示为:

Enter a number between 5 and 10: 
www eee
That is not a number. Please try again: 
That is not a number. Please try again: 
qqq 6
That is not a number. Please try again: 
You entered: 6  

我想要的是,如果用户输入例如“ qqq 6”或任何“输入空格输入”,则代码会告诉用户它无效并再次输入。

这可能吗?

5 个答案:

答案 0 :(得分:0)

将您的“ in”变量放入字符串[]变量中,然后检查每个元素是否为整数

答案 1 :(得分:0)

这是使用正则表达式的另一种方法:

import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class valin {

  private static final Pattern INT_5_TO_10 = Pattern.compile("[5-9]|10");

  public static void main(String[] args)
  {
    int inputNumber;

    Scanner in = new Scanner(System.in);

    System.out.println("Enter a number between 5 and 10: ");

    while(in.hasNextLine()){
      String line = in.nextLine().trim();
      Matcher m = INT_5_TO_10.matcher(line);

      if(m.matches()){
        inputNumber = Integer.parseInt(line);   //Only neccessary if you want to use this later on, otherwise we don't need to parse the number
        System.out.println("You entered: " + inputNumber);
        break;
      }else{
        System.out.println("That is not a number between 5 and 10. Please try again.");
      }
    }

    in.close();

  }

}

答案 2 :(得分:0)

您的代码存在问题

inputNumber = in.nextInt();

输入的带有空格的输入将被读取并解析两次。 因此您可以使用

String input = in.nextLine();

接受用户的输入,然后使用

将此字符串输入解析为Integer。
inputNumber = Integer.parseInt(input);

这样您就可以克服上述问题。

代码Integer.parseInt(input)可能会导致NumberFormatException。请处理必要的例外。

答案 3 :(得分:0)

这是一个捕获NumberFormatException的工作版本,正如其他一些人所建议的那样:

import java.util.Scanner;

public class valin {

  public static void main(String[] args)
  {
    Integer inputNumber = -1;

    Scanner in = new Scanner(System.in);

    System.out.println("Enter a number between 5 and 10: ");

    while(in.hasNextLine()){
      String line = in.nextLine().trim();

      try{
        inputNumber = Integer.parseInt(line);
      }catch (NumberFormatException nfe){
        System.out.println("That is not a number. Please try again.");
        continue;
      }

      if(inputNumber >= 5 && inputNumber <= 10){
        System.out.println("You entered: " + inputNumber);
        break;
      }else{
        System.out.println("That is not a number between 5 and 10. Please try again.");
      }
    }

    in.close();

  }

}

答案 4 :(得分:0)

这是@James Baker提供的解决方案的“更严格”版本,该解决方案在正则表达式中使用捕获组并尝试使用资源

import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Main {

    private static final Pattern INT_5_TO_10 = Pattern.compile("([5-9]|10)");

    private static int parse(String line) {
        Matcher m = INT_5_TO_10.matcher(line.trim());
        return m.matches() ? Integer.parseInt(m.group(1)) : -1;
    }

    public static void main(String[] args) {
        int inputNumber;
        try (Scanner in = new Scanner(System.in)) {
            System.out.println("Enter a number between 5 and 10: ");

            while (in.hasNextLine()) {
                String line = in.nextLine();
                inputNumber = parse(line);

                if (inputNumber > 0) {
                    System.out.println("You entered: " + inputNumber);
                } else {
                    System.out.println("That is not a number between 5 and 10. Please try again.");
                }
            }
        }
    }
}