I havent programmed in java for a few months now, been learning some other languages, but i start school in the fall for java so i thought i would fresh'n up abit. Tried to make a simple java game of hangman but its not working. Here is the code:
now here is the error:import java.util.Random; import java.util.Scanner; public class Hangman { public static void main(String[] args) { // TODO Auto-generated method stub String[] words = {"donkey", "moose", "horn", "nicole", "connor", "taco", "meenu", "brad", "fridge", "dust"}; Random rand = new Random(); Scanner scan = new Scanner(System.in); ///start of play again int wordN = rand.nextInt(words.length); String word = words[wordN]; char[] letters = new char [word.length()]; int strikes = 8; String[] characters = {"_ ", "_ ","_ ","_ ","_ ","_ ","_ "}; boolean won = false; //printing word length for (int i = 0; i<=word.length(); i++){ System.out.print(characters[i]); } //getting each letter for (int a=0; a<=word.length(); a++){ letters[a] = word.charAt(a); } do{ System.out.print("Guess a letter: "); String guess = scan.next(); char cGuess = guess.charAt(0); for (int b = 0; b<= word.length(); b++){ int iLetters = word.length(); if (cGuess == letters[b] && b<iLetters){ characters[b] = cGuess+" "; }if (cGuess != letters[b] && b==iLetters){ strikes--; System.out.println("The letter "+cGuess+" is not in this word. You have "+strikes+" trys left."); } } for (int i = 0; i<=word.length(); i++){ System.out.print(characters[i]); } if (guess.equals(word)){ won = true; } }while(won != true); } }
any help would be lovely, thanksException in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 4 at java.lang.String.charAt(Unknown Source) at Hangman.main(Hangman.java:26)