防止扫描程序读取Java中的剩余输入

防止扫描程序读取Java中的剩余输入,java,java.util.scanner,Java,Java.util.scanner,我想知道如何才能阻止扫描仪读取来自用户的额外错误输入 例如,我想将用户输入的2one作为2读入变量diam,下面的程序实现了这一点 接下来的问题是,下一个循环自动检测输入中剩余的one,并相应地执行if语句 我可以通过创建两个扫描仪来解决这个问题,但不幸的是,这个特殊任务不允许这样做。此外,我们需要在程序中使用.hasnetint() 如何仅使用一台扫描仪防止“溢出”?我不得不假设这个问题以前有人提出过,但我没有太多的运气找到答案 Scanner scnr = new Scanner(S

我想知道如何才能阻止扫描仪读取来自用户的额外错误输入

例如,我想将用户输入的
2one
作为
2
读入变量
diam
,下面的程序实现了这一点

接下来的问题是,下一个循环自动检测输入中剩余的
one
,并相应地执行
if
语句

我可以通过创建两个扫描仪来解决这个问题,但不幸的是,这个特殊任务不允许这样做。此外,我们需要在程序中使用
.hasnetint()

如何仅使用一台扫描仪防止“溢出”?我不得不假设这个问题以前有人提出过,但我没有太多的运气找到答案

    Scanner scnr = new Scanner(System.in);

    System.out.print("Enter the diameter of a "
            + "cylinder (in centimeters): ");
    // BEGIN: diameter input verification loop      
    for (;;) {
        if (!scnr.hasNextInt()) {
            System.out.print("Please enter an integer value "
                    + "(less than 2,147,483,648) as decimal digits: ");
            scnr.nextLine(); 
            continue;
        }
        diam = scnr.nextInt();
        if (diam >= 0) {
           //null statement
        } 
        else {
          System.out.print("Please enter a positive integer value: ");
          continue;
        }
    break;
    }
    //END: diameter input verification loop

    //prompts user for container height     
    System.out.print("Enter the height of a "
            + "cylinder (in centimeters): ");

    // BEGIN: height input verification loop        
    for (;;) {
        if (!scnr.hasNextInt()) {
            System.out.print("Please enter an integer value "
                    + "(less than 2,147,483,648) as decimal digits: ");
            scnr.nextLine(); 
            continue;
        }
        height = scnr.nextInt();
        if (height >= 0) {
           //null statement
        } 
        else {
          System.out.print("Please enter a positive integer value: ");
          continue;
        }
    break;
    }
    //END: height input verification loop`

一种选择是只从
扫描仪
读取整行输入,然后只保留第一个单词。例如,对于圆柱体的直径,可以使用:

System.out.print("Enter the diameter of a cylinder (in centimeters): ");
String input = scnr.nextLine();
try {
    int diam = Integer.parseInt(input.split(" ")[0]);
}
catch (NumberFormatException e) {
    System.out.print("Please enter an integer value "
                + "(less than 2,147,483,648) as decimal digits: ");
}

一种选择是只从
扫描仪
读取整行输入,然后只保留第一个单词。例如,对于圆柱体的直径,可以使用:

System.out.print("Enter the diameter of a cylinder (in centimeters): ");
String input = scnr.nextLine();
try {
    int diam = Integer.parseInt(input.split(" ")[0]);
}
catch (NumberFormatException e) {
    System.out.print("Please enter an integer value "
                + "(less than 2,147,483,648) as decimal digits: ");
}

我可以想出几种方法:

  • 正如Tim所指出的,您可以使用
    readLine()
    从用户那里读取完整的行,然后解析该行。您可以使用
    split
    ,或者创建一个新的扫描程序来解析该行,或者使用各种其他方法

  • 您可以使用一个
    扫描仪
    并调用
    nextLine()。显然,您需要在调用
    nextInt()
    后执行此操作。例如:

    height = -1;
    while (height < 0) {
        if (!scnr.hasNextInt()) {
            System.out.print("Please enter an integer value "
                    + "(less than 2,147,483,648) as decimal digits: ");
        } else {
            height = scnr.nextInt();
            if (height < 0) {
               System.out.print("Please enter a positive integer value: ");
            }
        }
        scanner.nextLine();
    }
    
    height=-1;
    而(高度<0){
    如果(!scnr.hasnetint()){
    System.out.print(“请输入一个整数值”
    +“(少于2147483648)作为十进制数字:”;
    }否则{
    高度=序号nextInt();
    如果(高度<0){
    System.out.print(“请输入正整数值:”);
    }
    }
    scanner.nextLine();
    }
    
    (您的代码的上述版本已对内容进行了重组,以消除
    中断
    继续
    。重组还允许我将readLine作为循环的无条件最后语句。我认为这使逻辑更容易理解…)


    • 我可以想出几种方法:

      • 正如Tim所指出的,您可以使用
        readLine()
        从用户那里读取完整的行,然后解析该行。您可以使用
        split
        ,或者创建一个新的扫描程序来解析该行,或者使用各种其他方法

      • 您可以使用一个
        扫描仪
        并调用
        nextLine()。显然,您需要在调用
        nextInt()
        后执行此操作。例如:

        height = -1;
        while (height < 0) {
            if (!scnr.hasNextInt()) {
                System.out.print("Please enter an integer value "
                        + "(less than 2,147,483,648) as decimal digits: ");
            } else {
                height = scnr.nextInt();
                if (height < 0) {
                   System.out.print("Please enter a positive integer value: ");
                }
            }
            scanner.nextLine();
        }
        
        height=-1;
        而(高度<0){
        如果(!scnr.hasnetint()){
        System.out.print(“请输入一个整数值”
        +“(少于2147483648)作为十进制数字:”;
        }否则{
        高度=序号nextInt();
        如果(高度<0){
        System.out.print(“请输入正整数值:”);
        }
        }
        scanner.nextLine();
        }
        
        (您的代码的上述版本已对内容进行了重组,以消除
        中断
        继续
        。重组还允许我将readLine作为循环的无条件最后语句。我认为这使逻辑更容易理解…)

      您只需添加一个
      scnr.nextLine()
      即可删除先前输入中遗留的“旧”
      scnr
      。这解决了溢出问题


      您只需添加一个
      scnr.nextLine()
      即可删除先前输入中遗留的“旧”
      scnr
      。这解决了溢出问题。

      感谢您的帮助,这似乎是一个简单的方法。虽然我忘了提到作业需要使用“.hasNextInt”,而且我相信它希望我们坚持迄今为止所学的基本循环。尽管如此,我不知道评分程序是否会将“尝试”和“抓住”标记为错误。很抱歉,我将编辑我的问题。@FofX如果您使用
      nextInt()
      ,根据定义,您只能读取一个标记。您的问题是,您希望处理用户输入多个令牌作为输入的场景。感谢您的帮助,这似乎是一种简单的方法。虽然我忘了提到作业需要使用“.hasNextInt”,而且我相信它希望我们坚持迄今为止所学的基本循环。尽管如此,我不知道评分程序是否会将“尝试”和“抓住”标记为错误。很抱歉,我将编辑我的问题。@FofX如果您使用
      nextInt()
      ,根据定义,您只能读取一个标记。您的问题是,您希望处理用户输入多个令牌作为输入的场景。