什么是 Java:NoSuchElementException 错误?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3852910/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 03:41:04  来源:igfitidea点击:

What is the Java: NoSuchElementException error?

javaexception

提问by D. Spigle

Looking for help with the following code...

正在寻找有关以下代码的帮助...

package pkgPeople;

import java.io.File;
import java.io.PrintWriter;
import java.util.Scanner;

public class CreateWithoutSerialization {

    public static void main(String[] args) throws Exception
    {
        BankAccount bankAccount = new BankAccount(0, 0);
        Person person = new Person();
        String nm;
        int ht;
        int wt;
        long ba;
        double bal;
        File inFile = new File("G:/CS9.27/inperson.txt");
        File outFile = new File("G:/CS9.27/outperson.txt");
        PrintWriter writer = new PrintWriter(outFile);
        Scanner reader = new Scanner(inFile);

        nm = reader.nextLine();
        ht = reader.nextInt();
        wt = reader.nextInt();
        ba = reader.nextLong();
        bal = reader.nextDouble();

        person.setName(nm);
        person.setHeight(ht);
        person.setWeight(wt);
        bankAccount.setAcctID(ba);
        bankAccount.setBalance(bal);


        System.out.println(person.toString());

        //Write the attributes in ASCII to a file
        writer.printf("%s is the name of the person.\r\n",nm);
        writer.printf("%d inches is the height of %s.\r\n",ht, nm);
        writer.printf("%d pounds is the weight of %s\r\n",wt,nm);
        writer.printf("%d dollars is the balance of %s\r\n", bal, nm);
        writer.printf("%l is the ID of the bank account.\r\n", ba);



    }

}


Upon running, i get this exception..

运行时,我收到此异常..

Exception in thread "main" java.util.NoSuchElementException: No line found
at java.util.Scanner.nextLine(Unknown Source)
at pkgPeople.CreateWithoutSerialization.main(CreateWithoutSerialization.java:23)


Is this a file error? Have tried multiple fixes but still stuck.

这是文件错误吗?尝试了多种修复,但仍然卡住。

回答by Stephen C

The exception is being thrown in the call to reader.nextLine()According to the javadoc, this means that nextLine()could not find a next line.

reader.nextLine()根据javadoc,在调用中抛出异常,这意味着nextLine()找不到下一行。

Based on a careful reading of the javadoc, I think that this means that your input file is empty. You could test this by calling hasNextLine()before the call to nextLine().

基于对 javadoc 的仔细阅读,我认为这意味着您的输入文件为空。您可以通过在调用hasNextLine()之前调用来测试这一点nextLine()

回答by Tim Bender

As a beginner in a programming language, you need to learn how to use the documentation and resources made readily available for that language.

作为编程语言的初学者,您需要学习如何使用该语言随时可用的文档和资源。

If you look at the javadoc for the method you are using hereyou would soon realize that the problem is that there is not a new line character for the Scannerto read in a line. Check your input file and make sure it meets the specifications. If you are sure your input file is correct you can do some debugging by using the file API to make sure the input File exists before attempting to use it as input for the Scanner.

如果您查看您在此处使用的方法的 javadoc,您很快就会意识到问题在于在一行中没有可供Scanner读取的换行符。检查您的输入文件并确保它符合规范。如果您确定您的输入文件是正确的,您可以使用文件 API 进行一些调试,以确保输入文件在尝试将其用作扫描仪的输入之前存在。

All of the information you need is readily available in the javadoc.

您需要的所有信息都可以在javadoc 中轻松获得。

回答by Favonius

Hmmm... I there are two things which are missing here....

嗯......我这里缺少两件事......

Now the NoSuchElementExceptioncomes when input is exhausted. It need not to be related to nextLine(). So check whether for the following number of reads

现在NoSuchElementException,当谈到输入信息已耗尽。它不需要与nextLine(). 所以检查是否为以下读取次数

nm = reader.nextLine();
ht = reader.nextInt();
wt = reader.nextInt();
ba = reader.nextLong();
bal = reader.nextDouble();

You have equal number of lines in your inperson.txt

您的 inperson.txt 中的行数相同

AlsoWhen done writing ... do this also..

当完成写作......也这样做......

writer.flush();
writer.close();

Otherwise you won't see any output file... :)

否则你将看不到任何输出文件... :)

good luck

祝你好运