2

I am basically writing a hangman game and I have the program read these four letter words from a .txt file I have. I have copied the .class and the .txt files over in to the src folder for the program and it still prints out the "File not found." that I have written in my RandomWordProvider class. Here is the code:

public class WordGuessingGame {

static class RandomWordProvider {

private final List<String> words;

public RandomWordProvider() {
    words = readFile();
}

private int randomInteger() {
    int randomInt = (int) (Math.random() * words.size());
    return randomInt;
}

public String getWord(int randomInt, String line) {
    int randomPosition = randomInteger();
    String randomWord = words.get(randomPosition);
    return randomWord;
}

private List<String> readFile() {

    List<String> wordsList = new ArrayList<>();

    try {
        String fileName = "FourLetterWords.txt";
        File fourLetterWords = new File(fileName);
        Scanner in = new Scanner(fourLetterWords);

        while (in.hasNextLine()) {
            String line = in.nextLine();
            if (line!=null && !line.isEmpty()) {
                wordsList.add(line);
            }
        }
    } catch (FileNotFoundException ex) {
        System.out.println("File not found.");
    }

    return wordsList ;
    }
}

static class PlayerCharacterEntry {

    public String playerEntry() {
        Scanner characterEntry = new Scanner(System.in);
        System.out.print("Enter a character: ");
        String playerInput = characterEntry.next();
        playerInput = playerInput.toUpperCase();
        return playerInput;
    }
}

public static void main(String[] args) {

Scanner wantToPlay = new Scanner(System.in);
System.out.print("Welcome to the word guessing game! Would you like to play? ");
String playerAnswer = wantToPlay.next();

if (playerAnswer.equals("Yes") || playerAnswer.equals("yes")) {
    System.out.print("\nYour objective is to guess a four letter word by entering"
            + "\nletters on your keyboard. If you can not guess the word in seven attempts,"
            + "\nyou lose! You will be told if the letter you entered is in the word, and"
            + "\nyou will be told if the letter you entered is not in the word. You will be"
            + "\nallowed to guess the word any time during your seven attempts. If at anytime"
            + "\nyou would like to terminate the game, enter the word 'terminate'. Good Luck!");
}
if (playerAnswer.equals("No") || playerAnswer.equals("no")) {
    System.out.print("Maybe another time!");
}
RandomWordProvider randomWordProvider = new RandomWordProvider();
PlayerCharacterEntry playerCharacterEntry = new PlayerCharacterEntry();

String playerInput;
String randomWord;


}
}

So obviously it is doing the "try" but it isn't working and it's just doing the file not found, but I don't know why. Can someone help me?

NEPat10
  • 159
  • 1
  • 2
  • 12
  • 3
    Your code isn't running from the *src* folder. It'll be compiled and probably run from the bin folder. You need to consider these files as resources relative to the runtime folder. See this for ideas: http://stackoverflow.com/questions/4871051/getting-the-current-working-directory-in-java – Dave Dec 18 '14 at 17:10

3 Answers3

1

If you don't want to specify a hard-coded path, here the idea to get file from your classpath:

        try {
            URL url = RandomWordProvider.class.getResource("FourLetterWords.txt");
            File fourLetterWords = new File(url.toURI());
            Scanner in = new Scanner(fourLetterWords);
            ....
        }
aparna
  • 333
  • 1
  • 8
0

try file name as

String fileName = "src/FourLetterWords.txt";
Bruce
  • 8,609
  • 8
  • 54
  • 83
0

Most likely your app is running from a different directory than you expect and your relative path is relative from the executable's directory. So you can try a few things.

  1. Copy your text file into the same directory as the exe and see if that solves the problem
  2. Use an absolute path to the file instead of relative. Backslashes need to be escaped "\". So this would look like:

String fileName = "C:\\myApps\\Hangman\\FourLetterWords.txt";

If either of those solves your problem, then it was just a path issue.

CasualCoder
  • 115
  • 1
  • 9