有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java NoTouchElementException无限循环与pmd DDA异常

我对编程有点陌生,对gradle和pmd插件也很陌生,所以请发发慈悲吧

如果用户输入了一个数字,请选择扫描仪。nextLine()将在每次迭代中抛出一个NoSuchElementException,从而创建一个无限循环

public class Console {
    public int readInteger(String line) {
        Integer x = null;
        while(x == null) {
            try(Scanner scanner = new Scanner(System.in) {
                System.out.print(line);
                x = scanner.nextInt();
            } catch(InputMismatchException exc) {
                  //error message
            } catch(InvalidStateException exc) {
                  //error message
            } catch(NoSuchElementException exc) {
                 //error message
            }
        }
        return x;    
    }
}

我将感激每一位伸出的援助之手

编辑:意识到,我的问题与扫描仪同时出现。nextLine()方法。顺序不重要,我的循环仍然是一个无限循环,具有相同的NoTouchElementException


共 (2) 个答案

  1. # 1 楼答案

    下面是完整的(可编译的)示例:

    import java.util.Scanner;
    import java.util.InputMismatchException;
    import java.util.NoSuchElementException;
    
    public class Console {
        public int readInteger(String line) {
            Integer x = null;
            while(x == null) {
                try(Scanner scanner = new Scanner(System.in)) {
                    System.out.print(line);
                    x = scanner.nextInt();
                } catch(InputMismatchException exc) {
                      //error message
                } catch(IllegalStateException exc) {
                      //error message
                } catch(NoSuchElementException exc) {
                     //error message
                }
            }
            return x;
        }
    
        public static void main(String[] args) {
            Console c = new Console();
            int age = c.readInteger("How old are you? ");
            System.out.printf("You are %d years old.%n", age);
        }
    }
    

    注意:InvalidStateException不存在,它是非法的StateException

    将此代码段保存在Console.java中,并使用类似于java Console.java的java 11+运行它

    如果您正在输入例如42,它会工作。如果不输入整数,则“无止境循环”开始,例如very old。现在我们需要实际处理异常。在这种情况下,InputMismatchException将被抛出。但是错误的输入不会被使用,并且仍在扫描程序中-因此使用nextInt()重试将再次引发相同的异常。在用户输入新数据之前,我们必须先读取错误的令牌。我们可以使用nextLine()读取数据,但因此我们需要访问扫描器,因此我们需要更早地打开扫描器实例,并在外部资源中进行循环和错误处理

    如果用户关闭输入流(在Windows下使用Ctlr+Z或在Linux下使用Cltr+D),那么NoSuchElementException将被抛出,因此我们也需要处理这种情况^如果扫描程序实例本身已关闭,则将抛出{}

    下面是一个固定的完整示例:

    import java.util.Scanner;
    import java.util.InputMismatchException;
    import java.util.NoSuchElementException;
    
    public class Console {
        public int readInteger(String line) {
            Integer x = null;
            try (Scanner scanner = new Scanner(System.in)) {
                while(x == null) {
                    try {
                        System.out.print(line);
                        x = scanner.nextInt();
                    } catch(InputMismatchException exc) {
                        String wrongInput = scanner.nextLine();
                        System.out.printf("The input '%s' is not a number. Please try again.%n", wrongInput);
                    } catch(NoSuchElementException exc) {
                       // no input provided
                       System.exit(1);
                    }
                }
            }
            return x;
        }
    
        public static void main(String[] args) {
            Console c = new Console();
            int age = c.readInteger("How old are you? ");
            System.out.printf("You are %d years old.%n", age);
        }
    }
    
  2. # 2 楼答案

    只要换个颜色就行了

    x = scanner.nextLine();
    

    x = scanner.nextInt();
    

    也不,它不会进入无限循环,因为您使用了错误的方法,它根本不会工作