作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
抱歉,如果这是一个愚蠢的问题,我是一个编程菜鸟,并且在使用以下代码时遇到了问题。
public static void main(String[] args) throws IOException {
addName();
addName(); //Line "10" in error
}
public static void addName() throws IOException {
//Initializing Variables and Scanner
Scanner keyboard = new Scanner(System.in);
FileWriter fwriter = new FileWriter("Data.txt", true);
PrintWriter outputFile = new PrintWriter(fwriter);
int numFriends;
String firstName;
String lastName;
String email;
//Get number of entries to add
System.out.print("How many people do you wish to add? ");
numFriends = Integer.parseInt(keyboard.nextLine()); //Line "25" in error
//Prompt for info and add it to the file
for(int i = 1; i <= numFriends; i++){
System.out.printf("Enter the first name of person %d: ",i);
firstName = keyboard.nextLine();
System.out.printf("Enter the last name of person %d: ",i);
lastName = keyboard.nextLine();
System.out.printf("Enter the email of person %d: ",i);
email = keyboard.nextLine();
outputFile.println(firstName + " " + lastName + " " + email);
}
//Wrap things up and give a confirmation
outputFile.close();
keyboard.close();
System.out.println("Data written to the file.");;
}
addName 方法在运行一次时有效,但在第二次运行时出现以下错误。
How many people do you wish to add? 1
Enter the first name of person 1: s
Enter the last name of person 1: d
Enter the email of person 1: g
Data written to the file.
How many people do you wish to add? Exception in thread "main"
java.util.NoSuchElementException: No line found
at java.util.Scanner.nextLine(Unknown Source)
at Problem1.addName(Problem1.java:25)
at Problem1.main(Problem1.java:10)
我最初认为这是来自一个未在某处消耗的换行符,但我似乎无法弄清楚。为什么我会收到此错误?我该如何修复它?谢谢!
最佳答案
您不应该在方法结束时关闭 Scanner
,理想情况下您应该在整个执行过程中重用相同的 Scanner
。
通过关闭Scanner
,您还将关闭其输入源,在本例中为System.in
。因此,用户不能再输入任何内容。
下面是更正后的代码片段:
public static void main(String[] args) throws IOException {
Scanner keyboard = new Scanner(System.in);
addName(keyboard);
addName(keyboard);
}
public static void addName(Scanner keyboard) throws IOException {
FileWriter fwriter = new FileWriter("Data.txt", true);
PrintWriter outputFile = new PrintWriter(fwriter);
int numFriends;
String firstName;
String lastName;
String email;
//Get number of entries to add
System.out.print("How many people do you wish to add? ");
numFriends = Integer.parseInt(keyboard.nextLine()); //Line "25" in error
//Prompt for info and add it to the file
for(int i = 1; i <= numFriends; i++){
System.out.printf("Enter the first name of person %d: ",i);
firstName = keyboard.nextLine();
System.out.printf("Enter the last name of person %d: ",i);
lastName = keyboard.nextLine();
System.out.printf("Enter the email of person %d: ",i);
email = keyboard.nextLine();
outputFile.println(firstName + " " + lastName + " " + email);
}
//Wrap things up and give a confirmation
outputFile.close();
System.out.println("Data written to the file.");
}
关于java - 第二次运行方法时出现"No such element"错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47501504/
我是一名优秀的程序员,十分优秀!