0

Hey everyone!
So, I'm making a hangman game. I'm at the very beginning stages so I just have a String array and just to make sure it works correctly I want to print a random word from the array - simple, right? I am running into an error - the output is below. I would be so happy if you guys could help!!!
here is the code - and yes Hangman2 is the correct name.

import java.util.*;

public class Hangman2 {
    public static void main(String [] args){
        Random random  = new Random();
        int rgen = random.nextInt(10)+1;
        System.out.println(rgen); //print random to test
        String [] wordList = new String [10];
        wordList[1] = "table";
        wordList[2] = "shed";
        wordList[3] = "computer";
        wordList[4] = "because";
        wordList[5] = "knight";
        wordList[6] = "table";
        wordList[7] = "hover";
        wordList[8] = "hat";
        wordList[9] = "racecar";
        wordList[10] = "scissors";
        System.out.println(wordList[rgen]);

    }
}

This is the output:

1
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 10
    at Hangman2.main(Hangman2.java:23)

Edited by mike_2000_17: Fixed formatting

1
Contributor
1
Reply
3
Views
6 Years
Discussion Span
Last Post by lprk94
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.