【问题标题】:Cannot declare global, static Scanner object without throwing FileNotFoundException?不能在不抛出 FileNotFoundException 的情况下声明全局静态 Scanner 对象?
【发布时间】:2020-08-04 23:26:01
【问题描述】:

基本上,我正在为我正在制作的一个简单的 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 对象是全局/静态的通常是不好的做法。我认为因为这个特殊的 Scanner 没有收集用户输入(而且我没有计划在完成后维护这个相对简单的项目),所以问题不会太大。

编辑:我不知道为什么,但是编辑不让我在这篇文章的开头加上“你好”,所以这里是......大家好:)

【问题讨论】:

  • main 是否抛出异常无关紧要。在第一个示例中,scanner 是 Tests 类的静态变量,而不是 main 方法。 Main 是可选的,该类不关心它。

标签: java compiler-errors java.util.scanner filenotfoundexception


【解决方案1】:

因为没有捕捉到异常而出现错误。 Scanner 实例是在 Tests 类的上下文中创建的,main 方法是不同的,因此在那里捕获它不会解决问题。

如果您想要解决方法,可以尝试以下方法:

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

【讨论】:

  • 不幸的是,这对我不起作用。我现在做了一个丑陋的解决方法,但无论如何谢谢。
猜你喜欢
  • 1970-01-01
  • 2023-04-04
  • 2015-09-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多