0

I'm having a problem with my hangman program. I really think what I need to do is beyond what I understand about java. Here's my code

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileNotFoundException;
import java.util.Scanner;
import java.util.Random;

public class HangmanProject
{
    public static void main(String[] args) throws FileNotFoundException
    {
        String scoreKeeper; // to keep track of score
        int guessesLeft; // to keep track of guesses remaining
        String[] wordList = new String[25];
        final Random generator = new Random();
        Scanner keyboard = new Scanner(System.in); // to read user's input
        System.out.println("Welcome to Nick Carfagno's Hangman Project!");
        // Create a scanner to read the secret words file
        Scanner wordScan = null;
        try
        {
            wordScan = new Scanner(new BufferedReader(new FileReader("words.txt")));
            while (wordScan.hasNext())
            {
            }
        }
        finally
        {
            if (wordScan != null)
            {
                wordScan.close();
            }
        }
        // get random word from array
        class pickRand
        {
            public String get(String[] wordList)
            {
                int rnd = generator.nextInt(wordList.length);
                return wordList[rnd];
            }
        }
        System.out.println(wordList);
    }
}

I was able to get the program to read a file and then print to screen, but I can't figure out how to store the words from file into an array. I not advanced at all, so please try and be as thorough as possible.

Mat
  • 202,337
  • 40
  • 393
  • 406
Curly5115
  • 103
  • 1
  • 3
  • 13
  • 5
    Possible duplicate of: [Java: Reading a file into an array](http://stackoverflow.com/questions/285712/java-reading-a-file-into-an-array?rq=1) – Baz Aug 24 '12 at 16:44
  • [Java Tutorial on Scanner](http://docs.oracle.com/javase/tutorial/essential/io/scanning.html). – assylias Aug 24 '12 at 16:47
  • 1
    This same thing has been posted like 5 times in the past week... Like exactly the same, only difference in between them is who's Hangman Project it prints out, lol – Alex Coleman Aug 24 '12 at 16:50
  • 1
    BTW: Instead of asking this question for the third time ([first](http://stackoverflow.com/questions/12103518/print-data-from-file-to-array), [second](http://stackoverflow.com/questions/12085421/java-reading-a-file-and-storing-string-into-an-array)), why don't you try to improve your initial question? – Baz Aug 24 '12 at 16:57
  • @Baz sorry about that I didnt realize i could edit my posts. Im new to this forum – Curly5115 Aug 24 '12 at 18:49

2 Answers2

1

1) What you've got so far looks pretty good :)

2) Since you don't know exactly how many or few words you'll have, you don't want an "array". You're probably better off with an "ArrayList". Arrays are "fixed". Lists are "variable".

3) For each "word" you read, just ".add()" it to your arraylist

Voila! Done.

Here's a complete example:

paulsm4
  • 114,292
  • 17
  • 138
  • 190
0

You need to save the read line in a String object and assign it to a certain field of the array. For example:

wordList[0] = myString;

This would assign the value of myString to the first field of your array.

Said Savci
  • 818
  • 4
  • 14
  • 28