什么是Java:NoSuchElementException错误?

D. *_*gle 0 java exception

寻求以下代码的帮助......

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);



    }

}
Run Code Online (Sandbox Code Playgroud)

运行时,我得到这个例外..

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

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

Ste*_*n C 5

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

基于仔细阅读javadoc,我认为这意味着您的输入文件为空.您可以在致电hasNextLine()前致电测试nextLine().