gpt4 book ai didi

java - 当程序尝试写入文件时出现错误?

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:14:31 26 4
gpt4 key购买 nike

问题是我写入文件的部分。每次我完成为控制台中的字符串赋值时,它都会给我一个错误,如下所示:

Exception in thread "main" java.lang.IllegalStateException: Scanner closed
at java,util,Scanner,ensureOpen(Unknown Source)
at java,util,Scanner,findWithinHorizon(Unknown Source)
at java,util,Scanner,hasNextLine(Unknown Source)
at mainPackage,MainClass,main(MainClass.java:71)

这是我的代码:

package mainPackage;

import java.util.Scanner;
import java.util.concurrent.ThreadLocalRandom;
import java.util.ArrayList;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.BufferedWriter;

public class MainClass {

private static FileWriter fw;
private static BufferedWriter out;
private static Scanner input;
private static Scanner file;
private static FileWriter fw2;
private static BufferedWriter out2;
private static Scanner file2;
private static ArrayList<String> questList;
private static ArrayList<String> ansList;

public static void main(String[] args) throws IOException , IllegalStateException{

fw = new FileWriter("C:/Users/ALAN_BARE/Desktop/Dictionary.txt" , true );
input = new Scanner(System.in);
file = new Scanner(new File("C:/Users/ALAN_BARE/Desktop/Dictionary.txt"));
out = new BufferedWriter(fw);
fw2 = new FileWriter("C:/Users/ALAN_BARE/Desktop/Dictionary2.txt" , true);
out2 = new BufferedWriter(fw2);
file2 = new Scanner(new File("C:/Users/ALAN_BARE/Desktop/Dictionary2.txt"));
questList = new ArrayList<String>();
ansList = new ArrayList<String>();

String engword;
String gerword;
String response;
int counter = 0;

//Loading the file data to the arraylist
while (file.hasNextLine()) {
questList.add(file.nextLine());
ansList.add(file2.nextLine());
}

file.close();
file2.close();

System.out.println("What do you want to do, register words or Play the game?");
response = input.nextLine();

//Getting the values of the strings via user input
if (response.contains("register")) {

System.out.println("Type yor english word:");
engword = input.nextLine();
System.out.println("Type the translation in German:");
gerword = input.nextLine();

//writing it to a file
if (file.hasNextLine()) {
out.newLine();
out.write(engword);
}
else {
out.write(engword);
}

if (file2.hasNextLine()) {
out2.newLine();
out2.write(gerword);
}
else {
out2.write(gerword);
}
}

//The same, just for a different response
else if (response.contains("Register")) {
System.out.println("Type yor english word:");
engword = input.next();
System.out.println("Type the translation in German:");
gerword = input.next();

if (file.hasNextLine()) {
out.newLine();
out.write(engword);
}
else {
out.write(engword);
}

if (file2.hasNextLine()) {
out2.newLine();
out2.write(gerword);
}
else {
out2.write(gerword);
}
}

out.close();
out2.close();

//Running the quiz game
if (response.contains("play")) {

while (counter <= 10 ) {
counter++;
int sel = ThreadLocalRandom.current().nextInt(0, questList.size() + 1);

System.out.println("The english word is :" + questList.get(sel));
System.out.println("Please type the translation in German:");
String answer = input.nextLine();

if (answer.contains(ansList.get(sel))) {
System.out.println("Correct!");
}
else {
System.out.println("Wrong! You Lose! If you want to play again, restart the app.");
break;
}
}
}

while (counter == 10) {
System.out.println("You Win The Game! If you want to play again, restart the app.");
counter++;
}
}
}

可能是什么问题?

最佳答案

在第 56 行关闭名为 fileScanner 对象后出现错误是正常的

file.close()

您得到的错误清楚地解决了问题。

Scanner closed

关于java - 当程序尝试写入文件时出现错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38160515/

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