1

This is for a homework assignment, for a game of Hangman. Now, I got the whole game working except for this part. Reading the dictionary list that the teacher provided.

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

    Scanner fileScan = new Scanner(new File(words.txt));

    List<String> dictionary = new ArrayList<String>();
    while (fileScan.hasNext()) {

        dictionary.add(fileScan.nextLine().toLowerCase());
    }
    for( int i = 0; i < dictionary.size(); i++) {

        System.out.println(dictionary.get(i));
    }
}

I separated this part from the code to be able to test it. I also made the dictionary file into just 5 words. When I hit run, it doesn't print out anything. Just a blank space.

VedantK
  • 9,728
  • 7
  • 66
  • 71
HeyZeusV
  • 15
  • 2

1 Answers1

0

words.txt must be quoted 'coz it's a string.

like this

 Scanner fileScan = new Scanner(new File("words.txt"));

also make sure that the file path of your txtfile is correct. you can use either ABSOLUTE PATH OR RELATIVE PATH

when printing all the data, you can also simply do it like this using foreach

for(String item:dictionary){
    System.out.println(item);
}
Community
  • 1
  • 1
Dyrandz Famador
  • 4,499
  • 5
  • 25
  • 40