1

I am developing a Hangman code for class & I am supposed to have three different levels of difficulties compiled of 20 different possible words/phrases to guess from. I have the three text documents contained in the same folder as my program, but for some reason I keep getting

java.io.FileNotFoundException: 
at java.io.FileInputStream.open0(Native Method)
at java.io.FileInputStream.open(FileInputStream.java:195)
at java.io.FileInputStream.<init>(FileInputStream.java:138)
at java.util.Scanner.<init>(Scanner.java:611)
at FinalProject.showfile(FinalProject.java:137)
at FinalProject.game(FinalProject.java:48)
at FinalProject.main(FinalProject.java:35)

I don't know what to do to correct this. I am new to coding and don't have a great understanding towards this. I've included the part of the code for which allows the user to select the level of their desire & the code which finds a random word from each file. If more is required, I can give the whole thing.

I've looked at many other threads regarding this same problem but my issue appears to differ from them all. Prior to me adding in the if statement with all the other levels, I simply had:

fileName = "lv1text.txt";

and that worked just fine. But now that I have included the if statement, this error has come up.

I have tried using the exact directory but that still gives the same error.

    private static int levelSelect (int levelSelection) throws IOException
{
    levelSelection = 0;
    System.out.println("What level of difficulty would you like to play?");
    System.out.println("Enter 1 for Easy\nEnter 2 for Medium\nEnter 3 for Hard");
    levelSelection = getInt("",1,3);
    return levelSelection;
}

private static String showfile(String fileName) throws IOException
{
    levelSelect(levelSelection);
    if (levelSelection == 1)
    {
        fileName = "lv1list.txt";
    }
    else if (levelSelection == 2)
    {
        fileName = "lv2list.txt";
    }
    else if (levelSelection == 3)
    {
        fileName = "lv3list.txt";
    } 
    Scanner inFile = new Scanner (new File (fileName));
    int i = 0; 
    String[] impWord = new String[20];
    while (inFile.hasNextLine())
    {
        impWord[i] = inFile.nextLine();
        i++;
    }
    inFile.close();
    int rNum = ((int)Math.floor((impWord.length-1)*Math.random()-1));
    fWord = impWord[rNum];
    return fWord;
}
Elijah
  • 11
  • 2

0 Answers0