我正在嘗試打開,關閉和寫入文件。每當我嘗試打開一個文件時,如果它不存在於我提供的程序告訴我的路徑中。如果存在,程序將讀取裏面的內容並顯示它。如果用戶不想查找文件,則可以選擇創建文件並填充數據。在文件I/O期間來自未知源的Java掃描程序異常
它工作到目前爲止,除了每當我讀取文件,它成功顯示數據後,我得到一個未知的源掃描異常。這是我到目前爲止的代碼:
import java.util.Scanner;
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
public class FileIO {
public static void main(String[] args) throws IOException{
Scanner input = new Scanner(System.in);
while (true){
System.out.println("1. Load file from path of your choosing.");
System.out.println("2. Answer eight questions and save the answers into a file.");
System.out.println("3. Exit the program.");
int userChoice = input.nextInt();
switch (userChoice){
case 1:
loadFile();
break;
case 2:
answerQuestions();
break;
case 3:
System.out.println("Goodbye!");
break;
}
}
}
public static void loadFile() throws IOException{
System.out.println("What is the file name you want to try to read?");
Scanner input = new Scanner(System.in);
File f = new File(input.nextLine());
if (f.exists() && !f.isDirectory()){
System.out.println("The file exists!");
Scanner inputFile = new Scanner(f);
while (inputFile.hasNext()){
String answer = inputFile.next();
System.out.println(answer);
}
input.close();
}
else {
System.out.println("File does not exist at that given path!");
}
}
public static void answerQuestions() throws IOException{
Scanner input = new Scanner(System.in);
System.out.println("What is your name?"); // 1
String name = input.nextLine();
System.out.println("What is your favorite color?"); // 2
String color = input.nextLine();
System.out.println("What is your favorite type of music?"); // 3
String music = input.nextLine();
System.out.println("What is your favorite place?"); // 4
String place = input.nextLine();
System.out.println("What is your favorite food?"); // 5
String food = input.nextLine();
System.out.println("What is your favorite book?"); // 6
String book = input.nextLine();
System.out.println("What is your favorite programming language?"); // 7
String language = input.nextLine();
System.out.println("Do you prefer laptop or desktop computers?"); // 8
String computer = input.nextLine();
System.out.println("All the questions are answered. Name the file you want to save the answers to: ");
File f = new File(input.nextLine());
if (f.exists()){
System.out.println("File already exists!");
return;
}
PrintWriter output = new PrintWriter(f);
output.print(name + "\n");
output.print(color + "\n");
output.print(music + "\n");
output.print(place + "\n");
output.print(food + "\n");
output.print(book + "\n");
output.print(language + "\n");
output.print(computer + "\n");
output.close();
}
}
這是錯誤:
Exception in thread "main" java.util.NoSuchElementException
at java.util.Scanner.throwFor(Unknown Source)
at java.util.Scanner.next(Unknown Source)
at java.util.Scanner.nextInt(Unknown Source)
at java.util.Scanner.nextInt(Unknown Source)
at Lab4.main(Lab4.java:18)
到底是什麼問題?我嘗試將第19行的掃描器輸入更改爲輸入2,但這不起作用。如果文件在檢查路徑時不存在,那麼該程序可以讓我選擇儘可能多的菜單選項,所以我猜測如果找到文件,在開始將東西打印到控制檯的部分,程序出了問題。但我不知道它會是什麼。我會很感激任何提示或提示,指導我處理這個錯誤。謝謝你的時間。
而例外是什麼? – immibis
對不起,我添加了錯誤。 – HandleThatError