简体   繁体   English

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

[英]Java Error - “Exception in thread ”main" java.util.NoSuchElementException

I am taking an introductory class in programming with Java, so please excuse my ignorance when it comes to programming concepts/terminology. 我正在学习Java编程入门课程,所以请原谅我对编程概念/术语的无知。 Part of my current assignment is to read an integer number (corresponding to length, in inches) and to convert it to centimeters, printing the final converted number as a floating point value. 我当前任务的一部分是读取一个整数(对应于长度,以英寸为单位)并将其转换为厘米,然后将最终转换后的数字打印为浮点值。 However, I always get an error message when trying to run the project (in Eclipse) that says: 但是,尝试运行项目(在Eclipse中)时,总是收到一条错误消息,内容为:

Exception in thread "main" java.util.NoSuchElementException
    at java.util.Scanner.throwFor(Unknown Source)
    at java.util.Scanner.next(Unknown Source)
    at java.util.Scanner.nextInt(Unknown Source)
    at java.util.Scanner.nextInt(Unknown Source)
    at project1.inches(project1.java:82)
    at project1.main(project1.java:17)

I have looked over my code several times, but can't seem to find out what the problem is -- Can anybody help me figure out how to fix this? 我已经查看了我的代码几次,但似乎无法找出问题所在-有人可以帮助我找出解决方法吗? I will include a snippet of the source code below: 我将在下面包含源代码的片段:

public static void inches() {
    //Define object variables
    int vinch;
    float vcent;

    System.out.println("\n*******************************************************\n");
    System.out.println("Inch to Centimeter Conversion\n\n------------------------\n");

    //Read length (inches)
    System.out.println("Enter a length (in inches):");
    Scanner scanInches = new Scanner(System.in);
    vinch = scanInches.nextInt();

    //Convert input from inches to centimeters & print as float value
    vcent = (float) (vinch * 2.84);
    System.out.println(vinch + " inches is equal to " + vcent + " centimeters.");

    scanInches.close();
}

Thanks for your help and once again, I apologize for such a trivial question! 多谢您的协助,对于这个琐碎的问题,我再次表示歉意!

**Note: I'm sure my code has errors or impracticalities in it...feel free to point these out, should you find any. **注意:我确定我的代码中有错误或不切实际...如果有发现,请随时指出。

You do not want to close System.in 您不想关闭System.in

  // scanInches.close();  comment this out

Also you should test for the presence of an int using 另外,您还应该使用以下命令检查是否存在int

if (scanInches.hasNextInt())
    vinch = scanInches.nextInt();

I don't seem to see any error when I run the code. 运行代码时,我似乎看不到任何错误。 Here is the code, I just tested it to see if it runs correctly and it does, I made only a few changes. 这是代码,我只是对其进行了测试,以查看它是否可以正常运行,并且只做了一些更改。

public class Main {

    public static void main(String[] args) {
        inches();
    }

    public static void inches() {
        //Define object variables
        int vinch = 0;
        float vcent;

        System.out.println("\n*******************************************************\n");
        System.out.println("Inch to Centimeter Conversion\n\n------------------------\n");

        //Read length (inches)
        System.out.println("Enter a length (in inches):");
        Scanner scanInches = new Scanner(System.in);

        if (scanInches.hasNextInt())
        {
            vinch = scanInches.nextInt();
        }

        //Convert input from inches to centimeters & print as float value
        vcent = (float) (vinch * 2.54);
        System.out.println(vinch + " inches is equal to " + vcent + " centimeters.");

    }
}

As you can see I removed the scanner close method from the bottom of the inches method as you don't want to close it. 如您所见,由于您不想关闭扫描仪的close方法,因此我从inch方法的底部删除了它。 I also added a check to see if there was nextInt to grab from the input of the user just to be safe. 为了安全起见,我还添加了一个检查以查看是否存在从用户输入中提取的nextInt。 Also according to google, the conversion rate is 2.54, not 2.84 so I changed that too. 另外根据谷歌,转换率是2.54,而不是2.84,所以我也改变了。

Let's dissect the error message the runtime helpfully provided: 让我们剖析运行时提供的错误消息:

Exception in thread "main" java.util.NoSuchElementException
    at java.util.Scanner.throwFor(Unknown Source)
    at java.util.Scanner.next(Unknown Source)
    at java.util.Scanner.nextInt(Unknown Source)
    at java.util.Scanner.nextInt(Unknown Source)
    at project1.inches(project1.java:82)

This means that your code, on line 82 of project1.java, invoked the method java.util.Scanner.nextInt(), which signaled an error condition by throwing a java.util.NoSuchElementException. 这意味着您的代码在project1.java的第82行上调用了方法java.util.Scanner.nextInt(),该方法通过引发java.util.NoSuchElementException来指示错误情况。

To figure out why the method throws this exception, let's read its documentation : 为了弄清楚该方法为什么抛出此异常,让我们阅读其文档

public int nextInt() 公共int nextInt()

Scans the next token of the input as an int. 将输入的下一个标记扫描为int。

An invocation of this method of the form nextInt() behaves in exactly the same way as the invocation nextInt(radix), where radix is the default radix of this scanner. 调用nextInt()形式的此方法的行为与调用nextInt(radix)的行为完全相同,其中radix是此扫描器的默认基数。

Returns: 返回值:

the int scanned from the input 从输入扫描的int

Throws: 抛出:

InputMismatchException - if the next token does not match the Integer regular expression, or is out of range InputMismatchException-如果下一个标记与Integer正则表达式不匹配或超出范围

NoSuchElementException - if input is exhausted NoSuchElementException-如果输入已用尽

IllegalStateException - if this scanner is closed IllegalStateException-如果此扫描仪已关闭

Therefore, a NoSuchElementException indicates that you tried to read another number, but System.in was exhausted. 因此,NoSuchElementException表示您尝试读取另一个数字,但是System.in已耗尽。

That's weird, because System.in is usually connected to the keyboard, which provides an inexhaustible supply of keypresses ... 这很奇怪,因为System.in通常连接到键盘,从而提供了不竭的按键输入...

The most likely cause is therefore that you launched the program without a console, which is a setting in your launch configuration: 因此,最可能的原因是您在没有控制台的情况下启动了程序,这是启动配置中的一项设置:

分配控制台复选框

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

相关问题 线程“主”中的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.util.NoSuchElementException? - Exception in thread “main” java.util.NoSuchElementException? 线程“主”中的异常java.util.NoSuchElementException - Exception in thread “main” java.util.NoSuchElementException Java错误“线程中的异常”主“java.util.NoSuchElementException” - Java Error “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 线程“main”中的异常 java.util.NoSuchElementException(如何修复错误) - Exception in thread "main" java.util.NoSuchElementException (how to fix error) 线程“主”中的异常java.util.NoSuchElementException错误 - Exception in thread “main” java.util.NoSuchElementException Error
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM