gpt4 book ai didi

文件 I/O 期间 Java 扫描仪出现未知来源异常

转载 作者:行者123 更新时间:2023-12-01 12:42:38 30 4
gpt4 key购买 nike

我正在尝试打开、关闭和写入文件。每当我尝试打开一个文件时,如果我提供的路径中不存在该文件,程序就会告诉我。如果存在,程序将读取其中的内容并显示它。如果用户不想查找文件,可以选择创建文件并用数据填充它。

到目前为止,除了每当我读取文件时,它都有效,在成功显示数据后,我收到未知源扫描仪异常。这是到目前为止我的代码:

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 行的扫描仪输入更改为 input2,但这不起作用。如果在检查路径时文件不存在,该程序允许我选择尽可能多的菜单选项,所以我猜测程序在找到文件时开始将内容打印到控制台的部分出现问题。但我不知道它会是什么。我将不胜感激任何指导我处理此错误的提示或提示。感谢您抽出时间。

最佳答案

Scanner input = new Scanner(System.in);
/* other code here */
input.close();

关闭扫描程序也会关闭它正在使用的流。您关闭了System.in。就像任何其他流一样,如果它关闭,您就无法使用它。

理想的解决方案是创建一个扫描仪一次(在程序开始时),然后在每次想要读取输入时使用它。

关于文件 I/O 期间 Java 扫描仪出现未知来源异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24969016/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com