0

I'm trying to create a Hangman game but I don't know how to properly initialize the wordDisplay array, it gives an error this array is supposed to print out underscores for letters of the word to guess and then the underscores get replaced by letters that were guessed by the player

public class Hangman
{
    static Scanner userInput = new Scanner(System.in);

    public static void main(String args[])
    {
        String strword = "wordo";
        char[] theWord = strword.toCharArray();
        int k = strword.length();
        int c;

        char[] wordDisplay;

        for(c=0;c<=k;c++)
        {
            wordDisplay[c] = '_';
        }

        int hang = 0;
        int i;

        while(hang<k+10)
        {

            System.out.println("type a letter");

            for(i=0;i<5;i++)
            {
                System.out.print( wordDisplay[i] );
            }
            char userLetter = userInput.next().charAt(0);
            for(i=0;i<=k;i++)
            {
                if(userLetter==theWord[i])
                {
                    System.out.println("a letter guessed");
                    wordDisplay[i] = theWord[i];
                }
                else
                {
                    hang++;
                }
            }


        }   
        System.out.println("you hang");
    }

}
Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
coldman1
  • 23
  • 8
  • 2
    `it gives an error`, which error? – SomeJavaGuy Dec 13 '16 at 13:31
  • Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 5 at test2.Hangman.main(Hangman.java:19) – coldman1 Dec 13 '16 at 13:31
  • 3
    I'm about to write Q&A how to write a Hangman game in Java. – xenteros Dec 13 '16 at 13:31
  • 1
    You only _declared_ `wordDisplay` but never _initialized_ it so its value will always be `null`. Change `char[] wordDisplay` to `char[] wordDisplay = new char[k]` then it should work. Also in your for-loop you need to change `<=` to `<` or you'd get an IndexOutOfBoundsException. – mammago Dec 13 '16 at 13:35
  • is this really Java? This code should not compile at all - the compiler is smart enough to complain about the uninitialized variable. – user85421 Dec 13 '16 at 14:39

3 Answers3

2

Instead of

    char[] wordDisplay;

    for(c=0;c<=k;c++)
    {
        wordDisplay[c] = '_';
    }

write:

    char[] wordDisplay = new char[k+1];
    for(c=0;c<=k;c++) {
        wordDisplay[c] = '_';
    }

or

    char[] wordDisplay = new char[k];
    for(c=0;c<k;c++) {
        wordDisplay[c] = '_';
    }

Each object in Java must be initialized.

xenteros
  • 15,586
  • 12
  • 56
  • 91
1

Initialize your array with the length you have

char[] wordDisplay = new char[k]

and change your for to:

for(c=0;c<k;c++)
  {
    wordDisplay[c] = '_';
  }

Keep in mind that your first index is 0

Kiogara
  • 617
  • 1
  • 6
  • 15
-1

You have not only to declare but to initialize the array. Since an array has a fixed size you should initialize it with it's size. That on the other hand is the length off the input string. So e.g:

char[] wordDisplay = new char[k];
Robin Koch
  • 716
  • 3
  • 12