Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: Simple hangman game error

  1. #1
    Junior Member
    Join Date
    Aug 2013
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Simple hangman game error

    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:
    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);
    	}
     
    }
    now here is the error:
    Exception 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)
    any help would be lovely, thanks


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Simple hangman game error

    This is the mistake:

    for (int i = 0; i<=word.length(); i++){

    I know you know better.

  3. #3
    Junior Member
    Join Date
    Aug 2013
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Simple hangman game error

    Quote Originally Posted by GregBrannon View Post
    This is the mistake:

    for (int i = 0; i<=word.length(); i++){

    I know you know better.
    I dont see how that is the mistake( I commented that out to try and still get same error). Also the error is pointing at this:
    letters[a] = word.charAt(a);
    yet i see no issue with that either

  4. #4
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Simple hangman game error

    Java arrays are zero-based. An array that holds 10 elements has indices 0 - 9. The same array has length 10. The error is the '<=' sign. If this is news to you, review Java arrays.

  5. The Following User Says Thank You to GregBrannon For This Useful Post:

    UpInSmoke (August 20th, 2013)

  6. #5
    Junior Member
    Join Date
    Aug 2013
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Simple hangman game error

    Nope i remember that just has been awhile since java, thank you for refreshing my memory

  7. #6
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Simple hangman game error

    You're welcome, but I don't think there's anything wrong with your memory. If my summary of Java array characteristics is not something you just know, even with an absence of a few months, then there's a problem with either the depth or breatdth of your Java experience, probably both. Review the basics to greater depth, including practicing with lots of code to really learn what you read about.

    This will also improve your debugging skills which are weak since you would comment out a for loop definition or not see the relationship between the loop control variable and the statements in the loop's body. Spend more time with for, while, and do/while loops.

Similar Threads

  1. Method error for checking and updating guess in a basic hangman game
    By lotusmind in forum What's Wrong With My Code?
    Replies: 12
    Last Post: April 13th, 2013, 10:03 PM
  2. Hangman Game
    By connorlm3 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 7th, 2013, 12:50 PM
  3. hangman game
    By candoa in forum What's Wrong With My Code?
    Replies: 1
    Last Post: December 10th, 2012, 10:10 PM
  4. Hangman game
    By candoa in forum What's Wrong With My Code?
    Replies: 3
    Last Post: December 9th, 2012, 10:07 PM
  5. Hangman Game
    By Snow_Fox in forum What's Wrong With My Code?
    Replies: 3
    Last Post: November 29th, 2010, 04:07 PM