我尝试从文件加载联系人
contacts.txt
。
这个程序曾经有效,但现在不行了。
Exception in thread "main" java.util.NoSuchElementException
at java.base/java.util.Scanner.throwFor(Scanner.java:937)
at java.base/java.util.Scanner.next(Scanner.java:1478)
at Main.loadContacts(Main.java:28)
at Main.main(Main.java:14)`
模型中还有两个类(
Contact
、ContactManager
)和一个文本文件(contact.txt
)。
这是课
Main
。
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.text.ParseException;
import java.util.Scanner;
import models.Contact;
import models.ContactManager;
class Main {
static ContactManager manager = new ContactManager();
public static void main(String[] args) {
try {
loadContacts("contacts.txt");
System.out.println("CONTACTS LOADED\n\n");
System.out.println(manager);
manageContact();
} catch (FileNotFoundException e) {
System.out.println(e.getMessage());
}
}
public static void loadContacts(String fileName) throws FileNotFoundException {
FileInputStream fis = new FileInputStream(fileName);
Scanner scan = new Scanner(fis);
while (scan.hasNextLine()) {
try {
Contact contact = new Contact(scan.next(), scan.next(), scan.next());
manager.addContact(contact);
} catch (ParseException e) {
System.out.println(e.getMessage());
}
}
scan.close();
}
public static void manageContact() {
Scanner scan = new Scanner(System.in);
while (true) {
System.out.println("Would you like to a) add another contact b) remove a contact c) exit");
String response = scan.nextLine();
if (response.equals("a")) {
System.out.println("\tName: ");
String name = scan.nextLine();
System.out.println("\tPhone number: ");
String phoneNumber = scan.nextLine();
System.out.println("\tBirth Date: ");
String birthDate = scan.nextLine();
if (name.isBlank() || phoneNumber.isBlank() || phoneNumber.length() < 5) {
System.out.println("\nThe input is not valid.");
} else {
try {
manager.addContact(new Contact(name, phoneNumber, birthDate));
} catch (ParseException e) {
System.out.println(e.getMessage());
} finally {
System.out.println("\n\nUPDATED CONTACTS\n\n" + manager);
}
}
} else if (response.equals("b")) {
System.out.println("Who would you like to remove?");
manager.removeContact(scan.nextLine());
System.out.println("\n\nUPDATED CONTACTS\n\n" + manager);
} else {
break;
}
}
scan.close();
}
}
我找不到错误。 我该如何修复这个未经检查的异常? 如果我必须扔
NoSuchElementException
,我必须在哪里添加这个?
假设您的输入文件
contacts.txt
的结构如下:
\n
(新行)或 \r\n
(换行、新行)结束请始终提供您的输入(链接),即使只是作为粗略简化的示例给出:
Chet Baker, Amsterdam, Netherlands
George Gershwin, Hollywood, USA
Java
Scanner
的方法 next()
读取下一个标记。如果没有下一个标记,则抛出 NoSuchElementException
。
使用
nextLine()
然后分线会比较安全。
这使您有机会打印和调试读取的行。
请参阅了解 Scanner 的 nextLine()、next() 和 nextInt() 方法。
nextLine
和 split
public static void loadContacts(String fileName) throws FileNotFoundException {
FileInputStream fis = new FileInputStream(fileName);
Scanner scan = new Scanner(fis);
while (scan.hasNextLine()) {
try {
// this was the original line that caused the NoSuchElement exception
//Contact contact = new Contact(scan.next(), scan.next(), scan.next());
String line = scan.nextLine(); // read the line as whole
System.out.println(line); // control what is read
String[] parts = line.split(","); // split the line, e.g. by comma
if (parts.length < 3) { // require at least 3 parts
break; // then stop this iteration and try next line
}
Contact contact = new Contact(part[0], part[1], part[2]);
manager.addContact(contact);
} catch (ParseException e) {
System.out.println(e.getMessage());
}
}
scan.close();
}