gpt4 book ai didi

java - 在不引发FileNotFoundException的情况下无法声明全局静态Scanner对象?

转载 作者:行者123 更新时间:2023-12-02 10:56:21 24 4
gpt4 key购买 nike

基本上,我正在为自己制作的一个简单的CLI游戏运行一些测试,并且最初,我的所有变量/对象都是在main()方法的开头声明的。现在,我必须创建一个使用某些相同对象/变量的新方法,因此我决定将所有变量移出main(),并将其设置为静态/全局。这样做时,我遇到了一个问题:尽管main()抛出FileNotFoundException,我还是收到以下错误消息:

Tests.java:14: error: unreported exception FileNotFoundException; must be caught or declared to be thrown
static Scanner boardReader = new Scanner(board);
这是我的代码,直到有问题的一行:
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Arrays;
import java.util.Scanner;

class Tests {

// GAME OBJECTS AND VARIABLES

// The following is used for reading, displaying, and writing to the game board.
static File board = new File("Board.txt");
static Scanner boardReader = new Scanner(board);
static FileWriter boardWriter = null;
static StringBuilder boardBuilder = new StringBuilder((int)board.length());

static String boardDisplay = null; // This String allows boardContents to print in a more readable way.
static String boardContents = null; // This is Board.txt represented as a String.

// There are more variables here than shown, but these are the ones relevant to my problem.

// GAMEPLAY

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

boolean gameIsOngoing = true;
while(gameIsOngoing) {

// boardContents String from boardBuilder StringBuilder
while (boardReader.hasNextLine()) {
boardContents = (boardBuilder.append(boardReader.nextLine() + System.lineSeparator()).toString());
}
// More irrelevant code here.
}
}
}
为了解决此问题,我尝试将使用boardReader的代码放入适当的try-catch中,如下所示:
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Arrays;
import java.util.Scanner;

class Tests {

// GAME OBJECTS AND VARIABLES

// The following is used for reading, displaying, and writing to the game board.
static File board = new File("Board.txt");
static FileWriter boardWriter = null;
static StringBuilder boardBuilder = new StringBuilder((int)board.length());

static String boardDisplay = null; // This String allows boardContents to print in a more readable way.
static String boardContents = null; // This is Board.txt represented as a String.

// There are more variables here than shown, but these are the ones relevant to my problem.

// GAMEPLAY

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

boolean gameIsOngoing = true;
while(gameIsOngoing) {

// boardContents String from boardBuilder StringBuilder
try (Scanner boardReader = new Scanner(board)) {
while (boardReader.hasNextLine()) {
boardContents = (boardBuilder.append(boardReader.nextLine() + System.lineSeparator()).toString());
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
// More irrelevant code here.
}
}
}
可以,但是由于不再将Scanner对象与所有其他对象一起列出,因此使我的代码更难理解。我想我的问题是,为什么第一个代码不起作用?类似的东西有可能工作吗?
我还想快速提及在研究此问题时,我看到一些事情说让Scanner对象成为全局/静态对象通常是不好的做法。我认为,由于此特定的扫描仪未收集用户输入(并且我没有计划在完成后维护此相对简单的项目),因此这不会有太大问题。
编辑:我不知道为什么,但是编辑器不允许我在本文的开头放置“Hello”,所以这里是...大家好:)

最佳答案

存在错误,因为未捕获到异常。 Scanner实例是在Tests类的上下文中创建的,main方法有所不同,因此在其中捕获它不会解决问题。
如果您要解决此问题,可以尝试执行以下操作:

static {
try {
boardReader = new Scanner(board);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}

关于java - 在不引发FileNotFoundException的情况下无法声明全局静态Scanner对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63256373/

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