简体   繁体   English

Java中线程“main”java.util.NoSuchElementException中的异常

[英]Exception in thread "main" java.util.NoSuchElementException in Java

this error seems to be a very common issue.这个错误似乎是一个非常普遍的问题。 I've looked up on other Stack Overflow posts that ask about this and tried to implement their solutions, but I'm still getting the same error.我查看了其他询问此问题的 Stack Overflow 帖子并尝试实施他们的解决方案,但我仍然遇到相同的错误。 The complete error is:完整的错误是:

Exception in thread "main" java.util.NoSuchElementException
        at java.base/java.util.Scanner.throwFor(Scanner.java:937)
        at java.base/java.util.Scanner.next(Scanner.java:1594)
        at java.base/java.util.Scanner.nextInt(Scanner.java:2258)
        at java.base/java.util.Scanner.nextInt(Scanner.java:2212)
        at src.file.main(file.java:29)

I'm sure there's something really simple I'm missing, but I can't quite find it as when I read through my code, the logic seems good.我确定我遗漏了一些非常简单的东西,但是当我阅读我的代码时,我无法完全找到它,逻辑似乎很好。 This is my file:这是我的文件:

public class file {
    
    public static void main(String[] args) throws IOException {

        int choice = 0;

        while(choice != 3) {

            System.out.println("1. enter info \n2. print info \n3.exit");

            Scanner myVal = new Scanner(System.in);

            System.out.println("Enter a choice: ");
            choice = myVal.nextInt(); //Line 29

            if(choice == 1) {
                enterInfo();
            }
            else if(choice == 2) {
                print();
            }
            else if(choice == 3) {
                System.exit(0);
            }
        }
    }

    static ArrayList<newType> studInfo = new ArrayList<src.newType>();

    static void enterInfo() {

        Scanner keyboard = new Scanner(System.in);

            System.out.println("Enter the information (Program year average lastname): ");
            String info = keyboard.nextLine().trim();
            if(info.isEmpty()) {
                System.out.println("Error, no input was made.");
                keyboard.close();
                return;
            }
            String[] tokens = info.split(" ");
            int size = tokens.length;
            String prgm = tokens[0];
            int yr = Integer.parseInt(tokens[1]);

            String lastname = tokens[3];
            Double avg = Double.parseDouble(tokens[2]);
            newType inf = new newType(prgm, yr, avg, lastname);
            studInfo.add(inf);
            System.out.println("Information added.");
            keyboard.close();
        return;
    }

Example input: math 5 76 Smith, this information is added to the arraylist of type newType where it can be printed, or another profile can be added.示例输入:math 5 76 Smith,此信息被添加到newType类型的newType中,可以在其中打印,或者可以添加另一个配置文件。

The program compiles without errors or warnings, and I can successfully run the program.程序编译没有错误或警告,我可以成功运行程序。 When I choose option 1, I enter all the information, in the correct format, to which I get the information added message, signaling it was a successful process.当我选择选项 1 时,我以正确的格式输入所有信息,然后我收到information added信息的消息,表明这是一个成功的过程。 After this message, the exception appears.在此消息之后,出现异常。 This leads me to believe the error doesn't actually lie within my enterInfo function, as I first thought, but rather when it reaches line 29 for the second time.这让我相信错误实际上并不存在于我的 enterInfo 函数中,正如我最初想到的那样,而是当它第二次到达第 29 行时。 I don't know how to fix this error, could anyone help?我不知道如何解决这个错误,有人可以帮忙吗? Thanks in advance!提前致谢!

Apart from the mistakes done in using Scanner, the standards followed in java is also missing.除了使用Scanner时犯的错误,java中遵循的标准也缺失了。

  1. Classname should start with caps (file should be as File, same with newType as well. It should be NewType)类名应以大写字母开头(文件应为 File,也应与 newType 相同。它应为 NewType)
  2. If you are naming any class then it should be a noun and so should be named as per the goal to be achieved in the program such as AddingNumbers, ReverseNumbers.如果您要命名任何类,那么它应该是一个名词,因此应该根据程序中要实现的目标命名,例如AddingNumbers、ReverseNumbers。

Pls refer here why we shouldn't use multiple scanner in a program : Why does closing a scanner seem to break new scanners?请参考这里为什么我们不应该在程序中使用多个扫描仪: 为什么关闭扫描仪似乎会破坏新的扫描仪?

I have made few changes in your code.Hope this works !!!我对您的代码做了一些更改。希望这有效!!!

// modified code // 修改后的代码

    public class File {
        
        static Scanner myVal = new Scanner(System.in);   // Use this scanner throughout the program
        static ArrayList<newType> studInfo = new ArrayList<src.newType>();
    
        public static void main(String[] args) throws IOException {
    
            int choice = 0;
    
            while (choice != 3) {
    
                System.out.println("1. enter info \n2. print info \n3.exit");
    
                System.out.println("Enter a choice: ");
                choice = myVal.nextInt(); // Line 29
    
                if (choice == 1) {
                    enterInfo();
                } else if (choice == 2) {
                    // print();
                } else if (choice == 3) {
                    System.exit(0);
                }
                
            }
            myVal.close();    // Can be closed once the need is over.
        }           
    
        static void enterInfo() {
    
    //      Scanner keyboard = new Scanner(System.in);    No need of multiple scanners.
    
            System.out.println("Enter the information (Program year average lastname): ");
            String info = myVal.nextLine().trim();
            if (info.isEmpty()) {
                System.out.println("Error, no input was made.");
    //          keyboard.close();
                return;
            }
            String[] tokens = info.split(" ");
            int size = tokens.length;
            String prgm = tokens[0];
            int yr = Integer.parseInt(tokens[1]);
    
            String lastname = tokens[3];
            Double avg = Double.parseDouble(tokens[2]);
             newType inf = new newType(prgm, yr, avg, lastname);
             studInfo.add(inf);
            System.out.println("Information added.");
    //      keyboard.close();
            return;
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 线程“主”中的异常java.util.NoSuchElementException? - Exception in thread “main” java.util.NoSuchElementException? 线程“主”中的异常java.util.NoSuchElementException - Exception in thread “main” java.util.NoSuchElementException 线程“主”中的异常java.util.NoSuchElementException - Exception in thread “main” java.util.NoSuchElementException 主线程java.util.NoSuchElementException中的异常 - Exception in main thread java.util.NoSuchElementException 线程“main”中的异常 java.util.NoSuchElementException - Exception in thread "main" java.util.NoSuchElementException java 新手 - 线程“main”中的异常 java.util.NoSuchElementException - New to java - Exception in thread “main” java.util.NoSuchElementException 线程“主”中的Java异常java.util.NoSuchElementException运行时错误 - Java Exception in thread “main” java.util.NoSuchElementException runtime error Java扫描器错误:线程“主”中的异常java.util.NoSuchElementException - Java Scanner Error: Exception in thread “main” java.util.NoSuchElementException Java错误-“线程“ main”中的异常” java.util.NoSuchElementException - Java Error - “Exception in thread ”main" java.util.NoSuchElementException 线程“main”中的异常 java.util.NoSuchElementException,Java 扫描器 - Exception in thread "main" java.util.NoSuchElementException, Java scanner
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM