Java:NoSuchElementException [英] Java: NoSuchElementException
问题描述
我开始只是在读取数据文件时进行练习。当我运行程序时,数据文件被读取,什么不是,但由于某种原因,我仍然得到一个NoSuchElementException,我的输出没有按照它应该的方式格式化。以下是发生的事情:
I'm beginning to do exercises with simply reading in a data file. When I run the program, the data file gets read and what not, but for some reason I still get a "NoSuchElementException" and my output is not formatted the way it is supposed to be. Here is what is going on:
我创建了一个如下所示的简单数据文件:
I created a simple data file that looks like this:
Barry Burd
Author
5000.00
Harriet Ritter
Captain
7000.00
Ryan Christman
CEO
10000.00
之后我写了一个简单的getter和setter程序(代码如下)。
After that I wrote a simple "getter" and "setter" program (code is below).
import static java.lang.System.out;
//This class defines what it means to be an employee
public class Employee {
private String name;
private String jobTitle;
public void setName(String nameIn) {
name = nameIn;
}
public String getName() {
return name;
}
public void setJobTitle(String jobTitleIn) {
jobTitle = jobTitleIn;
}
public String getJobTitle() {
return jobTitle;
}
/*The following method provides the method for writing a paycheck*/
public void cutCheck(double amountPaid) {
out.printf("Pay to the order of %s ", name);
out.printf("(%s) ***$", jobTitle);
out.printf("%,.2f\n", amountPaid);
}
}
足够简单。 然后我写了实际使用这个东西的程序(下面的代码)。
Easy enough. Then I wrote the program that actually uses this stuff (code below).
import java.util.Scanner;
import java.io.File;
import java.io.IOException;
public class DoPayroll {
public static void main(String[] args) throws IOException {
Scanner diskScanner = new Scanner(new File("EmployeeInfo.txt"));
for (int empNum = 1; empNum <= 3; empNum++) {
payOneEmployee(diskScanner);
}
}
static void payOneEmployee(Scanner aScanner) {
Employee anEmployee = new Employee();
anEmployee.setName(aScanner.nextLine());
anEmployee.setJobTitle(aScanner.nextLine());
anEmployee.cutCheck(aScanner.nextDouble());
aScanner.nextLine();
}
}
这是我的输出:
Pay to the order of Exception in thread "main" java.util.NoSuchElementException: No line found
at java.util.Scanner.nextLine(Scanner.java:1516)
at DoPayroll.payOneEmployee(DoPayroll.java:25)
at DoPayroll.main(DoPayroll.java:14)
Barry Burd ( Author) ***$5,000.00
Pay to the order of Harriet Ritter ( Captain) ***$7,000.00
Pay to the order of Ryan Christman ( CEO) ***$10,000.00
编辑我发现了问题,但我不明白lol。显然,我必须在数据文件的末尾添加一个空行....为什么??
EDIT I found the problem but I don't understand it lol. Apparently, I had to add a blank line to the end of the data file....why??
推荐答案
我发现了问题,但我不明白它哈哈。显然,我必须在数据文件的末尾添加一个空行....为什么??
I found the problem but I don't understand it lol. Apparently, I had to add a blank line to the end of the data file....why??
因为你在告诉扫描器
在输入文件中的工资之后将有一个回车符。但是,对于最后一个条目,没有回车符,因此它确定您发出了编程错误并抛出了未经检查的异常。
Because you are telling the Scanner
that there will be a carriage return after the salary in the input file. However, for the last entry, there was no carriage return and so it determined that you had made a programming error and threw an unchecked exception.
您可以解决此问题您的代码如下:
You could solve the problem in your code like so:
static void payOneEmployee(Scanner aScanner) {
Employee anEmployee = new Employee();
anEmployee.setName(aScanner.nextLine());
anEmployee.setJobTitle(aScanner.nextLine());
anEmployee.cutCheck(aScanner.nextDouble());
if (aScanner.hasNextLine()) {
aScanner.nextLine();
}
}
这篇关于Java:NoSuchElementException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!