Alright, I have a hangman game due thursday at midnight and I've decided to start from scratch for various reasons.
So what I want to do is this. I want to have a text file with one word per line, I want to go in and count the number of words, and then create an array sized as needed and store the words in it.
The reason I want to do this is because, in the future instead of having to go in and modify code, someone could just attach a new text file and be done. If they wanted to use a bigger word bank, it wouldn't require modifying the code.
After that I want to randomly pick a word and create the hangman interface. I know we are suppose to use panels and I am not entirely sure how to go about that logic. I think the easiest way so far would be to just break the word into individual letters and generate 2 sets of Jpanels. One to be the hangman itself And then the other set to be the word. The word's Jpanels would be created based on the number of letters and for every time guessed right.. the panel would change from - to whatever letter. Although I'm still debating on how best to program that part. If guessed wrong +1 to a counter and reveal a Jpanel of hangman. If counter reaches 5 "game over"
The problem I'm having is actually more towards the beginning.
I am trying to think of how best to get the number of lines.
package Hangman; import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; import java.io.File; public class WordBank { public static int wordCount() { int x = 0; try { File words = new File("words.txt"); FileReader reader = new FileReader(words); BufferedReader in = new BufferedReader(reader); String string; while ((string = in.readLine()) != null) { x++; } in.close(); } catch (IOException e) { e.printStackTrace(); } return x; } }
Is what I have... the only problem is it seems like its counter productive to repeat almost the exact same process again, just to add the words in the second time.
Is there a better way to count the number of words in the text document?