Java 已引发的FileNotFoundException

Java 已引发的FileNotFoundException,java,exception,compiler-errors,Java,Exception,Compiler Errors,我这里有一个小代码(整体的一部分)用于一个刽子手游戏,以及它在编译时给出的错误 import java.lang.*; import java.io.*; import java.util.*; public class Hangman { //Properties private final int MAX_TRIES = 6; private StringBuffer secretWord; private StringBuffer allLetter

我这里有一个小代码(整体的一部分)用于一个刽子手游戏,以及它在编译时给出的错误

import java.lang.*;
import java.io.*;
import java.util.*;

public class Hangman {

    //Properties

    private final int MAX_TRIES = 6;
    private StringBuffer secretWord; 
    private StringBuffer allLetters;
    private StringBuffer usedLetters;
    private int numberOfIncorrectTries;
    private int maxAllowedIncorrectTries;
    private StringBuffer knownSoFar;

    //Constructor   

    public Hangman() 
    {
        numberOfIncorrectTries = 0;
        maxAllowedIncorrectTries = MAX_TRIES;
        allLetters = new StringBuffer("abcdefghijklmnopqrstuvwxyz");
        usedLetters = new StringBuffer("");
        secretWord = chooseSecretWord();          //THIS IS LINE 33
        knownSoFar = new StringBuffer("");

        for (int count = 0; count < secretWord.length(); count++)
        {
            knownSoFar.append("*");
        }
    }


    //Methods

    public StringBuffer chooseSecretWord() throws FileNotFoundException{

        File file = new File("words.txt");
        Scanner scanner = new Scanner(file);

        final int NUMBER_OF_WORDS;
        int counter;
        int wordIndex;

        NUMBER_OF_WORDS = 99;

        StringBuffer[] words = new StringBuffer[NUMBER_OF_WORDS];

        //move all words to words array
        counter = 0; 
        while(scanner.hasNext())
        {
            StringBuffer newWord = new StringBuffer(scanner.next());
            words[counter++] = newWord;
        }
        //Find a random integer to get random index of array
        wordIndex = (int)(Math.random()*NUMBER_OF_WORDS);

        return words[wordIndex];
    } 

我想找一个小时的原因,但什么都看不见。words.txt文件与程序位于同一文件夹中,并选择在主函数中使用的secretword()方法(该函数是为测试它而构建的)。我怀疑这是一个路径问题,但不知道如何解决它。提前感谢您的帮助。

FileNotFoundException是一个选中的异常,这意味着您需要捕获或抛出它。这取决于你的设计,如果你要扔或捕捉,但它需要做的地方

所以你可以扔到这里:

public Hangman() throws FileNotFoundException 
或在此处捕获:

allLetters = new StringBuffer("abcdefghijklmnopqrstuvwxyz");
usedLetters = new StringBuffer("");
try {
    secretWord = chooseSecretWord();
} catch (FileNotFoundException e) {
    // TODO Do something here, example log the error our present a error message to the user.
}          //THIS IS LINE 33
knownSoFar = new StringBuffer("");
如果您想了解更多关于异常的信息;然后,docs.oracle.com提供了关于异常的优秀教程:


FileNotFoundException是一个选中的异常,这意味着您需要捕获或抛出它。这取决于你的设计,如果你要扔或捕捉,但它需要做的地方

所以你可以扔到这里:

public Hangman() throws FileNotFoundException 
或在此处捕获:

allLetters = new StringBuffer("abcdefghijklmnopqrstuvwxyz");
usedLetters = new StringBuffer("");
try {
    secretWord = chooseSecretWord();
} catch (FileNotFoundException e) {
    // TODO Do something here, example log the error our present a error message to the user.
}          //THIS IS LINE 33
knownSoFar = new StringBuffer("");
如果您想了解更多关于异常的信息;然后,docs.oracle.com提供了关于异常的优秀教程:


在方法ChooseSectword()中抛出FileNotFoundException。并使用Constructor中的方法。因此,您必须执行以下操作:

public Hangman() throws FileNotFoundException{
     ....
}

或者在Constructor中捕获它。

在方法ChooseSectword()中抛出FileNotFoundException。并使用Constructor中的方法。因此,您必须执行以下操作:

public Hangman() throws FileNotFoundException{
     ....
}

或者在Constructor中捕获它。

这是一个编译错误。与路径无关。你需要了解编译和运行时之间的区别。作为一个初学者,我现在想不出其他任何东西。胡说。路径问题阻止您执行Java。它们不会导致编译错误。您需要的所有信息都在错误消息中。你不需要想出不相关的解释,即使是初学者,你也需要有不止一个想法。这是一个编译错误。与路径无关。你需要了解编译和运行时之间的区别。作为一个初学者,我现在想不出其他任何东西。胡说。路径问题阻止您执行Java。它们不会导致编译错误。您需要的所有信息都在错误消息中。你不需要想出不相关的解释,即使是初学者,你也需要有不止一个想法。啊,但它解决了!我不知道构造器也需要这种东西。谢谢@MaciejCygan你错了。@EJP Yes apoogies:),出于某种原因,我的测试床上仍然有异常。UpvotedAh,但它解决了!我不知道构造器也需要这种东西。谢谢@MaciejCygan你错了。@EJP Yes apoogies:),出于某种原因,我的测试床上仍然有异常。向上投票