简体   繁体   English

java 新手 - 线程“main”中的异常 java.util.NoSuchElementException

[英]New to java - Exception in thread “main” java.util.NoSuchElementException

I'm new to Java - I've looked but can't seem to find whats wrong with this code.我是 Java 新手 - 我已经看过了,但似乎找不到这段代码有什么问题。

import java.util.*;

class MyClass {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        System.out.println("Please input grade");
        int grade = scan.nextInt();
        String gradeLevel = "Error";

        if ((grade > 100) || (grade < 0)) {
            System.out.println("Sorry please put a number between 0-100");
        } else if (grade > 90) {
            gradeLevel = "A";
        } else if (grade > 75) {
            gradeLevel = "B";
        } else if (grade > 49) {
            gradeLevel = "C";
        } else {
            gradeLevel = "F";
        }
        System.out.println("This student recieved " + gradeLevel + " for his/her grade");
    }
}

This is the error i'm getting:这是我得到的错误:

> Exception in thread "main" java.util.NoSuchElementException
    at java.util.Scanner.throwFor(Scanner.java:862)
    at java.util.Scanner.next(Scanner.java:1485)
    at java.util.Scanner.nextInt(Scanner.java:2117)
    at java.util.Scanner.nextInt(Scanner.java:2076)
    at MyClass.main(Main.j`

Code is working perfectly fine in Eclipse IDE, I'm using, with the most of the inputs I'm giving.代码在 Eclipse IDE 中工作得非常好,我正在使用,并提供了大部分输入。 I think you just look for whether System.in is available by using我想你只是通过使用来寻找 System.in 是否可用

System.out.println(System.in.available()); System.out.println(System.in.available());

or use a generic function like scan.next();或者使用像 scan.next() 这样的通用函数;

Note: just close resources like Scanner classes, even though it will not cause any errors注意:只需关闭像 Scanner 类这样的资源,即使它不会导致任何错误

What you are seeing is an exception because System.in has an "end of file" status.您看到的是一个例外,因为 System.in 具有“文件结尾”状态。 Here you are trying to get Input before you type.在这里,您尝试在键入之前获取 Input。

  1. Just try scan.next () to resolve.就试试scan.next ()来解决。

  2. Please use a better IDE to resolve the expectations automatically.请使用更好的 IDE 来自动解决期望。

Initially it felt strange why you are getting this error, because I don't get it in my Eclipse IDE, but finally figured it out, I guess you should be using online IDE or something.最初为什么会出现此错误感到很奇怪,因为我在 Eclipse IDE 中没有得到它,但最终想通了,我想您应该使用在线 IDE 或其他东西。 You have to supply the input before starting the program.您必须在启动程序之前提供输入。 See the below screenshots and how I supply the value.请参阅下面的屏幕截图以及我如何提供该值。

在此处输入图片说明

在此处输入图片说明

在此处输入图片说明

声明:本站的技术帖子网页,遵循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.util.nosuchelementException中的异常带有新的Scanner(file) - exception in thread main java.util.nosuchelementexception with new Scanner(file) 线程“主”中的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中的异常 - Exception in thread "main" java.util.NoSuchElementException in Java Java错误-“线程“ main”中的异常” java.util.NoSuchElementException - Java Error - “Exception in thread ”main" java.util.NoSuchElementException
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM