java.lang.ArrayIndexOutOfBoundsException: 10 error hangman

  • (2 Pages)
  • +
  • 1
  • 2

17 Replies - 579 Views - Last Post: 05 May 2013 - 04:59 PM Rate Topic: -----

#1 Tigers27  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 56
  • Joined: 25-March 13

java.lang.ArrayIndexOutOfBoundsException: 10 error hangman

Posted 02 May 2013 - 11:04 PM

Hi, I'm very new to java and need some help. I keep getting that stupid error and have no idea why.

import java.io.*; //for file
import java.util.*; //for scanner

public class Lab65 {
  public static void main(String[] args)throws FileNotFoundException{
  
    
    
    int wrong = 0; // keeps track of user guesses
    String word = readwords();
    boolean temp = false;
    
    String[] words = new String[word.length()];
    String[] words2 = new String[word.length()];
    
   //methods to be used
    userGuess(word, words, words2);
    printhm(wrong);
   
 
  
  //introduce user to game
    System.out.println("Welcome to hangman!");
     System.out.println("Begin guessing letters!");
     System.out.println("These letters have already been guessed!");
     userGuess(words);
    
       //keep track of wrong user guesses
     while(wrong <= 6) {
       temp = add(words, words2);
       if(temp = false) {
         wrong++;
         
         printhm(wrong);
         System.out.println("Incorrect guess!" + (6 - wrong) + " more guesses left!");
         userGuess(words);
         System.out.println("These letters have already been guessed!");
         System.out.println("Make another guess!");
         
       }
       
       else if(temp == true) {    //user guess is correct
         System.out.println("These letters have already been guessed!");
         userGuess(words);
         System.out.println("Make another guess!");
         
        
         
       }
     }
  }
        //display hangman
  public static void printhm(int wrong) {
    if(wrong == 1){
     
    //hangman display 
    if(wrong == 0) {
      System.out.println(" ------");
      System.out.println("|      |");
      System.out.println("       |");
      System.out.println("       |");
      System.out.println("       |");
      System.out.println("       |");
    }
     else if (wrong == 1){
     System.out.println(" --------");
     System.out.println("|        |");
     System.out.println("0        |");
     System.out.println("         |");
     System.out.println("         |");
     System.out.println("         |");
    }
    else if (wrong == 2) {
      System.out.println(" --------");
      System.out.println("|        |");
      System.out.println("0        |");
      System.out.println("|        |");
      System.out.println("         |");
      System.out.println("         |");
    }
    else if (wrong == 3) {
      System.out.println(" --------");
      System.out.println("|        |");
      System.out.println("0        |");
      System.out.println("/|       |");
      System.out.println("         |");
      System.out.println("         |");
    }
    else if (wrong == 4){
      System.out.println(" ---------");
      System.out.println("|         |");
      System.out.println("0         |");
      System.out.println("/|\\      |");
      System.out.println("          |");
      System.out.println("          |");
    }
      else if (wrong == 5){
       System.out.println(" ---------");
       System.out.println("|         |");
       System.out.println("0         |");
       System.out.println("/|\\      |");
       System.out.println("/         |");
       System.out.println("          |");
      }
      else {
        
        System.out.println(" ---------");
        System.out.println("|         |");
        System.out.println("0         |");
        System.out.println("/|\\      |");
        System.out.println("/ \\      |");
        System.out.println("          |");
        System.out.println( "You lost! Better luck next time!!" );
      }}
    }
    
    
    // keep track of wrong guesses
  public static String readwords() throws FileNotFoundException {
  Scanner input = new Scanner(new File("hangman.txt")); 
    String[] word = new String[10]; 
    int count = 0;
    while (input.hasNext()){
      String x = input.next();
      word[count] = x;
      count++;
    }
    
    Random rand = new Random();
    int r = rand.nextInt(10);
    String rword = word[r];
    
    return rword;
  }

  public static void userGuess(String y, String[] m, String[] words2){
    for (int i=0; i < y.length(); i++){
      m[i] = "-";
    }
    for (int x = 0; x < y.length(); x++){
      if (x + 1 < y.length())
        words2[x] = y.substring(x, x + 1);
      else
        words2[x] = y.substring(x, y.length());
    }
  }
 
  public static boolean add(String[] m, String[] ma) {  //find if letters are in the word
    Scanner console = new Scanner(System.in);
    String letters = console.next();
    boolean found = false;
    
    for(int i = 0; i < ma.length; i++) {
      if(ma [i].equals(letters)){
        m[i] = letters;
        found = true;
      }
    }
    if(found) {
      return true;
    }
    else {
      return false;
    }
  }
  
  public static void userGuess(String[] m) {
    for(int i = 0; i < m.length; i++) {
      System.out.print(m[i]);
    }
   
  
  
  
  }
  

  /*  public static void playAgainMethod() {
    Scanner console = new Scanner(System.in);
    int response = console.nextInt();
    while (response != 2) {  

            //your game code   

          while (response != 1 && response != 2)  {  

                 System.out.println("Make a selection");  
                 System.out.println("============");  
                 System.out.println("1. Play again");
                 System.out.println("2. Quit"); 
                 System.out.println("============"); 

                if (response < 1 || response > 2) {  
               System.out.println("Please choose either 1 or 2");       
           */      
     
          
    
    }


    
        


     



















Is This A Good Question/Topic? 0
  • +

Replies To: java.lang.ArrayIndexOutOfBoundsException: 10 error hangman

#2 Gungnir  Icon User is offline

  • Your Imaginary Friend

Reputation: 152
  • View blog
  • Posts: 527
  • Joined: 21-May 11

Re: java.lang.ArrayIndexOutOfBoundsException: 10 error hangman

Posted 03 May 2013 - 01:16 AM

Could we see your Trace Stack?

Where do you split the file into individual words? I'm not seeing that part of the code.

As far as I can tell -- without seeing the file -- there are two places where this could occur:
//LINE 125
word[count] = x; //Could easily throw an OutOfBOundsException
//LINES 130-131
int r = rand.nextInt(10); //should be netInt(9);
String rword = word[r]; //word[10] does not exist



So for now, with the information given here, my best suggestion is a fairly simple one:
  public static String readwords() throws FileNotFoundException {
  Scanner input = new Scanner(new File("hangman.txt")); 
    String[] word = new String[10];
    String rword = ""
    int count = 0;
    while (input.hasNext()){
        String x = input.next();
        try {
            word[count] = x;
        } catch(IndexOutOfBoundsException e) {
            e.printStackTrace();
            break;
        }
        count++;
    }
    
    Random rand = new Random();
    int r = rand.nextInt(9);
    rword = word[r];
    
    return rword;
  }


Was This Post Helpful? 1
  • +
  • -

#3 Tigers27  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 56
  • Joined: 25-March 13

Re: java.lang.ArrayIndexOutOfBoundsException: 10 error hangman

Posted 03 May 2013 - 01:26 AM

View PostGungnir, on 03 May 2013 - 01:16 AM, said:

Could we see your Trace Stack?

Where do you split the file into individual words? I'm not seeing that part of the code.

As far as I can tell -- without seeing the file -- there are two places where this could occur:
//LINE 125
word[count] = x; //Could easily throw an OutOfBOundsException
//LINES 130-131
int r = rand.nextInt(10); //should be netInt(9);
String rword = word[r]; //word[10] does not exist



So for now, with the information given here, my best suggestion is a fairly simple one:
  public static String readwords() throws FileNotFoundException {
  Scanner input = new Scanner(new File("hangman.txt")); 
    String[] word = new String[10];
    String rword = ""
    int count = 0;
    while (input.hasNext()){
        String x = input.next();
        try {
            word[count] = x;
        } catch(IndexOutOfBoundsException e) {
            e.printStackTrace();
            break;
        }
        count++;
    }
    
    Random rand = new Random();
    int r = rand.nextInt(9);
    rword = word[r];
    
    return rword;
  }


Thanks for the help! Is there any way I can change line 125 so that way that is the only thing I need to fix? Your code works but we haven't covered that way of doing it yet in class so I'm worried I'd get points off.
Was This Post Helpful? 0
  • +
  • -

#4 Bountyhunter1234  Icon User is offline

  • D.I.C Head

Reputation: 19
  • View blog
  • Posts: 73
  • Joined: 12-May 09

Re: java.lang.ArrayIndexOutOfBoundsException: 10 error hangman

Posted 03 May 2013 - 01:41 AM

also you might wanna check your boolean statement in the main method you created an infinite loop, your scanner method will also add two letters if word has two of the same letters EX. green- if i type "e" both of the e's will show up, and finally you need a check for the word to see if it has been guessed because right now its just another infinite loop

for me i would use an Array List instead of an array when getting stuff from an file because it doesn't need to know how many are in it.
Was This Post Helpful? 1
  • +
  • -

#5 Gungnir  Icon User is offline

  • Your Imaginary Friend

Reputation: 152
  • View blog
  • Posts: 527
  • Joined: 21-May 11

Re: java.lang.ArrayIndexOutOfBoundsException: 10 error hangman

Posted 03 May 2013 - 02:14 AM

Quote

Thanks for the help! Is there any way I can change line 125 so that way that is the only thing I need to fix? Your code works but we haven't covered that way of doing it yet in class so I'm worried I'd get points off.


Well, beyond catching the exception, you can attempt to prevent it using a counter and some if statements:

public static String readwords() throws FileNotFoundException {
  Scanner input = new Scanner(new File("hangman.txt")); 
  String[] word = new String[10];
  String rword = "";

  //Hopefully you've covered FOR loops :)/>/>
  for(int i = 0; i < word.length; i++)
  {
    if (input.hasNext())
      word[i] = input.next();
    //if you haven't covered breaks, just take out these two tines
    else
        break;
  }
  
  Random rand = new Random();
  int r = rand.nextInt(9);
  rword = word[r];
  
  return rword;
}



bountyhunter1234 said:

also you might wanna check your boolean statement in the main method you created an infinite loop


I'm sorry, but please explain your reasoning? No loops rely on that boolean variable.

This post has been edited by Gungnir: 03 May 2013 - 02:15 AM

Was This Post Helpful? 1
  • +
  • -

#6 Bountyhunter1234  Icon User is offline

  • D.I.C Head

Reputation: 19
  • View blog
  • Posts: 73
  • Joined: 12-May 09

Re: java.lang.ArrayIndexOutOfBoundsException: 10 error hangman

Posted 03 May 2013 - 03:07 AM

sorry this is what i meant
while(wrong <= 6) {
       temp = add(words, words2);
       if(temp = false) {
         wrong++;

hes taking temp and changing it to false instead of checking for false which created an infinite loop
Was This Post Helpful? 2
  • +
  • -

#7 Gungnir  Icon User is offline

  • Your Imaginary Friend

Reputation: 152
  • View blog
  • Posts: 527
  • Joined: 21-May 11

Re: java.lang.ArrayIndexOutOfBoundsException: 10 error hangman

Posted 03 May 2013 - 03:43 AM

Ahh, I see. You're absolutely right, Bountyhunter1234. It should be:
while(wrong <= 6) {
       temp = add(words, words2);
       if(temp == false) { // == operator
         wrong++;


Was This Post Helpful? 1
  • +
  • -

#8 Tigers27  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 56
  • Joined: 25-March 13

Re: java.lang.ArrayIndexOutOfBoundsException: 10 error hangman

Posted 03 May 2013 - 10:04 AM

thanks so much for all the help guys, it's working now!! both of you gave me exactly what I needed!
There's one last thing I need help with and that is figuring out how to stop the game when the user wins or loses. Basically I don't know how to have a congratulatory method or how to get it to cut off and tell them they lost. Oh and I just checked I also needed it to print out the letters they've already guessed. for some reason, that part isn't working
import java.io.*; //for file
import java.util.*; //for scanner

public class Lab65 {
  public static void main(String[] args)throws FileNotFoundException{
  
    
    
    int wrong = 0; // keeps track of user guesses
    String word = readwords();
    boolean temp = false;
    
    String[] words = new String[word.length()];
    String[] words2 = new String[word.length()];
    
   //methods to be used
    userGuess(word, words, words2);
    printhm(wrong);
   
 
  
  //introduce user to game
    System.out.println("Welcome to hangman!");
     System.out.println("Begin guessing letters!");
     System.out.println("These letters have already been guessed!");
     userGuess(words);
    
       //keep track of wrong user guesses
     while(wrong <= 6) {  
     temp = add(words, words2);  
     if(temp == false) { // == operator  
       wrong++;
         
         printhm(wrong);
         System.out.println("Incorrect guess!" + (6 - wrong) + " more guesses left!");
         userGuess(words);
         System.out.println("These letters have already been guessed!");
         System.out.println("Make another guess!");
         
       }
       
       else if(temp == true) {    //user guess is correct
         System.out.println(" These letters have already been guessed!");
         userGuess(words);
         System.out.println("Make another guess!");
         
        
         
       }
     }
  }
        //display hangman
  public static void printhm(int wrong) {
    if(wrong == 1){
     
    //hangman display 
    if(wrong == 0) {
      System.out.println("  ------");
      System.out.println(" |      |");
      System.out.println("        |");
      System.out.println("        |");
      System.out.println("        |");
      System.out.println("        |");
    }
     else if (wrong == 1){
     System.out.println("  --------");
     System.out.println(" |        |");
     System.out.println(" 0        |");
     System.out.println("          |");
     System.out.println("          |");
     System.out.println("          |");
    }
    else if (wrong == 2) {
      System.out.println("  --------");
      System.out.println(" |        |");
      System.out.println(" 0        |");
      System.out.println(" |        |");
      System.out.println("          |");
      System.out.println("          |");
    }
    else if (wrong == 3) {
      System.out.println("  --------");
      System.out.println(" |        |");
      System.out.println(" 0        |");
      System.out.println("/|        |");
      System.out.println("          |");
      System.out.println("          |");
    }
    else if (wrong == 4){
      System.out.println("  ---------");
      System.out.println(" |         |");
      System.out.println(" 0         |");
      System.out.println("/|\\       |");
      System.out.println("           |");
      System.out.println("           |");
    }
      else if (wrong == 5){
       System.out.println("  ---------");
       System.out.println(" |         |");
       System.out.println(" 0         |");
       System.out.println("/|\\       |");
       System.out.println("/          |");
       System.out.println("           |");
      }
      else {
        
        System.out.println("  ---------");
        System.out.println(" |         |");
        System.out.println(" 0         |");
        System.out.println("/|\\       |");
        System.out.println("/ \\       |");
        System.out.println("           |");
        System.out.println( "You lost! Better luck next time!!" );
      }}
    }
    
    
    // keep track of wrong guesses
 public static String readwords() throws FileNotFoundException {  

   Scanner input = new Scanner(new File("hangman.txt"));   

   String[] word = new String[10];  

   String rword = "";  
 
   for(int i = 0; i < word.length; i++)  

   {  
     if (input.hasNext())  

    word[i] = input.next();   

    else 
  break;  

  }        

   Random rand = new Random();  

  int r = rand.nextInt(9);  

  rword = word[r];  
   return rword;  
 } 

  
  public static void userGuess(String y, String[] m, String[] words2){
    for (int i=0; i < y.length(); i++){
      m[i] = "-";
    }
    for (int x = 0; x < y.length(); x++){
      if (x + 1 < y.length())
        words2[x] = y.substring(x, x + 1);
      else
        words2[x] = y.substring(x, y.length());
    }
  }
 
  public static boolean add(String[] m, String[] ma) {  //find if letters are in the word
    Scanner console = new Scanner(System.in);
    String letters = console.next();
    boolean found = false;
    
    for(int i = 0; i < ma.length; i++) {
      if(ma [i].equals(letters)){
        m[i] = letters;
        found = true;
      }
    }
    if(found) {
      return true;
    }
    else {
      return false;
    }
  }
  
  public static void userGuess(String[] m) {
    for(int i = 0; i < m.length; i++) {
      System.out.print(m[i]);
    }
   
  
  
  
  }
  

  /*  public static void playAgainMethod() {
    Scanner console = new Scanner(System.in);
    int response = console.nextInt();
    while (response != 2) {  

            //your game code   

          while (response != 1 && response != 2)  {  

                 System.out.println("Make a selection");  
                 System.out.println("============");  
                 System.out.println("1. Play again");
                 System.out.println("2. Quit"); 
                 System.out.println("============"); 

                if (response < 1 || response > 2) {  
               System.out.println("Please choose either 1 or 2");       
           */      
     
          
    
    }












This post has been edited by Tigers27: 03 May 2013 - 10:09 AM

Was This Post Helpful? 0
  • +
  • -

#9 Gungnir  Icon User is offline

  • Your Imaginary Friend

Reputation: 152
  • View blog
  • Posts: 527
  • Joined: 21-May 11

Re: java.lang.ArrayIndexOutOfBoundsException: 10 error hangman

Posted 03 May 2013 - 10:24 AM

Well, you've already set the conditions for the user losing.
     //keep track of wrong user guesses
     while(wrong <= 6) { 
       //omitted for brevity
     }
     System.out.println("GAME OVER!");



But you need to find out whether or not the user has won by checking their answers against the current word. Also, consider only taking characters as input instead of Strings, as this will make the process a lot easier.
Was This Post Helpful? 0
  • +
  • -

#10 Tigers27  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 56
  • Joined: 25-March 13

Re: java.lang.ArrayIndexOutOfBoundsException: 10 error hangman

Posted 03 May 2013 - 10:57 AM

View PostGungnir, on 03 May 2013 - 10:24 AM, said:

Well, you've already set the conditions for the user losing.
     //keep track of wrong user guesses
     while(wrong <= 6) { 
       //omitted for brevity
     }
     System.out.println("GAME OVER!");



But you need to find out whether or not the user has won by checking their answers against the current word. Also, consider only taking characters as input instead of Strings, as this will make the process a lot easier.



thanks for the help already. I've been running it a few times and there are two little weird things that I've been trying to fix but haven't been able.
The first is that it is not printing out the letters the user already guessed which is weird.
The next thing is that I'm somehow screwing up my code before the hangman just a little bit. When the user enters a correct guess, it prints out "incorrect guess" but the weird thing is that the logistics all work
The last thing is that it doesn't cut off when the game is over. When the user wins, it keeps running. And when the user loses, it keeps running.

These are my last few minor issues and I'm almost done thankfully, I've spent too much time on this.


import java.io.*; //for file
import java.util.*; //for scanner

public class Lab65 {
  public static void main(String[] args)throws FileNotFoundException{
  
    
    
    int wrong = 0; // keeps track of user guesses
    String word = readwords();
    boolean temp = false;
    
    String[] words = new String[word.length()];
    String[] words2 = new String[word.length()];
    
   //methods to be used
    userGuess(word, words, words2);
    printhm(wrong);
   
 
  
  //introduce user to game
    System.out.println("Welcome to hangman!");
     System.out.println("Begin guessing letters!");
     System.out.println("These letters have already been guessed!");
     userGuess(words);
    
     //keep track of wrong user guesses  

 while(wrong <= 6) {   
 temp = add(words, words2);
 
 
 if(temp == false) { 
   wrong++;
 

 }  
         printhm(wrong);
         System.out.println("Incorrect guess!" + (6 - wrong) + " more guesses left!");
         userGuess(words);
         System.out.println(" These letters have already been guessed!");
         System.out.println("Make another guess!");
         
       }
       
       if (temp == true) {    //user guess is correct
         System.out.println("Correct guess!");
         System.out.println(" These letters have already been guessed!");
         userGuess(words);
         System.out.println("Make another guess!");
         
        
         
       }
     }
  
        //display hangman
  public static void printhm(int wrong) {
  
     
    //hangman display 
    if(wrong == 0) {
      System.out.println("  ------");
      System.out.println(" |      |");
      System.out.println("        |");
      System.out.println("        |");
      System.out.println("        |");
      System.out.println("        |");
    }
     else if (wrong == 1){
     System.out.println("  --------");
     System.out.println(" |        |");
     System.out.println(" 0        |");
     System.out.println("          |");
     System.out.println("          |");
     System.out.println("          |");
    }
    else if (wrong == 2) {
      System.out.println("  --------");
      System.out.println(" |        |");
      System.out.println(" 0        |");
      System.out.println(" |        |");
      System.out.println("          |");
      System.out.println("          |");
    }
    else if (wrong == 3) {
      System.out.println("  --------");
      System.out.println(" |        |");
      System.out.println(" 0        |");
      System.out.println("/|        |");
      System.out.println("          |");
      System.out.println("          |");
    }
    else if (wrong == 4){
      System.out.println("  ---------");
      System.out.println(" |         |");
      System.out.println(" 0         |");
      System.out.println("/|\\       |");
      System.out.println("           |");
      System.out.println("           |");
    }
      else if (wrong == 5){
       System.out.println("  ---------");
       System.out.println(" |         |");
       System.out.println(" 0         |");
       System.out.println("/|\\       |");
       System.out.println("/          |");
       System.out.println("           |");
      }
      else {
        
        System.out.println("  ---------");
        System.out.println(" |         |");
        System.out.println(" 0         |");
        System.out.println("/|\\       |");
        System.out.println("/ \\       |");
        System.out.println("           |");
        System.out.println( "You lost! Better luck next time!!" );
      }
    
  }
    // keep track of wrong guesses
 public static String readwords() throws FileNotFoundException {  
 Scanner input = new Scanner(new File("hangman.txt"));   
 String[] word = new String[10];  
    String rword = "";  
 
   for(int i = 0; i < word.length; i++)  

   {  
     if (input.hasNext())  

    word[i] = input.next();   

    else 
  break;  

  }        

   Random rand = new Random();  

  int r = rand.nextInt(9);  

  rword = word[r];  
   return rword;  
 } 

  
  public static void userGuess(String y, String[] m, String[] words2){
    for (int i=0; i < y.length(); i++){
      m[i] = "-";
    }
    for (int x = 0; x < y.length(); x++){
      if (x + 1 < y.length())
        words2[x] = y.substring(x, x + 1);
      else
        words2[x] = y.substring(x, y.length());
    }
  }
 
  public static boolean add(String[] m, String[] ma) {  //find if letters are in the word
    Scanner console = new Scanner(System.in);
    String letters = console.next();
    boolean found = false;
    
    for(int i = 0; i < ma.length; i++) {
      if(ma [i].equals(letters)){
        m[i] = letters;
        found = true;
      }
    }
    if(found) {
      return true;
    }
    else {
      return false;
    }
  }
  
  public static void userGuess(String[] m) {
    for(int i = 0; i < m.length; i++) {
      System.out.print(m[i]);
    }
   
  
  
  
  }
  

  /*  public static void playAgainMethod() {
    Scanner console = new Scanner(System.in);
    int response = console.nextInt();
    while (response != 2) {  

            //your game code   

          while (response != 1 && response != 2)  {  

                 System.out.println("Make a selection");  
                 System.out.println("============");  
                 System.out.println("1. Play again");
                 System.out.println("2. Quit"); 
                 System.out.println("============"); 

                if (response < 1 || response > 2) {  
               System.out.println("Please choose either 1 or 2");       
           */      
     
          
    
    }









Was This Post Helpful? 0
  • +
  • -

#11 Bountyhunter1234  Icon User is offline

  • D.I.C Head

Reputation: 19
  • View blog
  • Posts: 73
  • Joined: 12-May 09

Re: java.lang.ArrayIndexOutOfBoundsException: 10 error hangman

Posted 04 May 2013 - 09:21 AM

import java.io.*; //for file
import java.util.*; //for scanner

public class Lab65 {
    static String userG = "";
    public static void main(String[] args)throws FileNotFoundException{

        int wrong = 0; // keeps track of user guesses
        String word = readwords();
        boolean temp = false;

        String[] words = new String[word.length()];
        String[] words2 = new String[word.length()];

        //methods to be used
        userGuess(word, words, words2);
        printhm(wrong);

        //introduce user to game
        System.out.println("Welcome to hangman!");
        System.out.println("Begin guessing letters!");
        System.out.println("These letters have already been guessed!");
        userGuess(words);

        //keep track of wrong user guesses  

        while(wrong <6) {   
            temp = add(words, words2);
            userGuess(words);

            if(temp == false) { 
                wrong++;
                printhm(wrong);
                System.out.println("Incorrect guess!" + (6 - wrong) + " more guesses left!");
                System.out.println();
                System.out.println(userG);
                
                System.out.println();
                System.out.println(" These letters have already been guessed!");
                System.out.println("Make another guess!");

            }else{    //user guess is correct
                printhm(wrong);
                System.out.println("Correct guess!");
                System.out.println(" These letters have already been guessed!");

                System.out.println(userG);
                System.out.println("Make another guess!");

            }
        }
    }
    //display hangman
    public static void printhm(int wrong) {

        //hangman display 
        if(wrong == 0) {
            System.out.println("\n  ------");
            System.out.println(" |      |");
            System.out.println("        |");
            System.out.println("        |");
            System.out.println("        |");
            System.out.println("        |");
        }
        else if (wrong == 1){
            System.out.println("  --------");
            System.out.println(" |        |");
            System.out.println(" 0        |");
            System.out.println("          |");
            System.out.println("          |");
            System.out.println("          |");
        }
        else if (wrong == 2) {
            System.out.println("  --------");
            System.out.println(" |        |");
            System.out.println(" 0        |");
            System.out.println(" |        |");
            System.out.println("          |");
            System.out.println("          |");
        }
        else if (wrong == 3) {
            System.out.println("  --------");
            System.out.println(" |        |");
            System.out.println(" 0        |");
            System.out.println("/|        |");
            System.out.println("          |");
            System.out.println("          |");
        }
        else if (wrong == 4){
            System.out.println("  ---------");
            System.out.println(" |         |");
            System.out.println(" 0         |");
            System.out.println("/|\\       |");
            System.out.println("           |");
            System.out.println("           |");
        }
        else if (wrong == 5){
            System.out.println("  ---------");
            System.out.println(" |         |");
            System.out.println(" 0         |");
            System.out.println("/|\\       |");
            System.out.println("/          |");
            System.out.println("           |");
        }
        else {

            System.out.println("  ---------");
            System.out.println(" |         |");
            System.out.println(" 0         |");
            System.out.println("/|\\       |");
            System.out.println("/ \\       |");
            System.out.println("           |");
            System.out.println( "You lost! Better luck next time!!" );
        }

    }
    // keep track of wrong guesses
    public static String readwords() throws FileNotFoundException {  
        Scanner input = new Scanner(new File("hangman.txt"));   
        String[] word = new String[10];  
        String rword = "";  

        for(int i = 0; i < word.length; i++){  
            if (input.hasNext())  {
                
                word[i] = input.next();    

            }else {
                break;  
                    
            }
        }        

        Random rand = new Random();  

        int r = rand.nextInt(9);  

        rword = word[r];  
        return rword;  
    } 

    public static void userGuess(String y, String[] m, String[] words2){
        for (int i=0; i < y.length(); i++){
            m[i] = "-";
        }
        for (int x = 0; x < y.length(); x++){
            if (x + 1 < y.length())
                words2[x] = y.substring(x, x + 1);
            else
                words2[x] = y.substring(x, y.length());
        }
    }

    public static boolean add(String[] m, String[] ma) {  //find if letters are in the word
        Scanner console = new Scanner(System.in);
        String letters = console.next();
        boolean found = false;

        for(int i = 0; i < ma.length; i++) {
            if(ma [i].equals(letters)){
                m[i] = letters;
                found = true;
            }
        }
        if(found) {
            return true;
        }
        else {
            return false;
        }
    }

    public static void userGuess(String[] m) {
        for(int i = 0; i < m.length; i++) { 
            System.out.print(m[i] );
        }

    }
 
}

/*  public static void playAgainMethod() {
Scanner console = new Scanner(System.in);
int response = console.nextInt();
while (response != 2) {  

//your game code   

while (response != 1 && response != 2)  {  

System.out.println("Make a selection");  
System.out.println("============");  
System.out.println("1. Play again");
System.out.println("2. Quit"); 
System.out.println("============"); 

if (response < 1 || response > 2) {  
System.out.println("Please choose either 1 or 2");       
 */      






ive fixed your if statement you closed it before the else ran so it would never get to true
Was This Post Helpful? 0
  • +
  • -

#12 Tigers27  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 56
  • Joined: 25-March 13

Re: java.lang.ArrayIndexOutOfBoundsException: 10 error hangman

Posted 04 May 2013 - 10:47 PM

View PostBountyhunter1234, on 04 May 2013 - 09:21 AM, said:

import java.io.*; //for file
import java.util.*; //for scanner

public class Lab65 {
    static String userG = "";
    public static void main(String[] args)throws FileNotFoundException{

        int wrong = 0; // keeps track of user guesses
        String word = readwords();
        boolean temp = false;

        String[] words = new String[word.length()];
        String[] words2 = new String[word.length()];

        //methods to be used
        userGuess(word, words, words2);
        printhm(wrong);

        //introduce user to game
        System.out.println("Welcome to hangman!");
        System.out.println("Begin guessing letters!");
        System.out.println("These letters have already been guessed!");
        userGuess(words);

        //keep track of wrong user guesses  

        while(wrong <6) {   
            temp = add(words, words2);
            userGuess(words);

            if(temp == false) { 
                wrong++;
                printhm(wrong);
                System.out.println("Incorrect guess!" + (6 - wrong) + " more guesses left!");
                System.out.println();
                System.out.println(userG);
                
                System.out.println();
                System.out.println(" These letters have already been guessed!");
                System.out.println("Make another guess!");

            }else{    //user guess is correct
                printhm(wrong);
                System.out.println("Correct guess!");
                System.out.println(" These letters have already been guessed!");

                System.out.println(userG);
                System.out.println("Make another guess!");

            }
        }
    }
    //display hangman
    public static void printhm(int wrong) {

        //hangman display 
        if(wrong == 0) {
            System.out.println("\n  ------");
            System.out.println(" |      |");
            System.out.println("        |");
            System.out.println("        |");
            System.out.println("        |");
            System.out.println("        |");
        }
        else if (wrong == 1){
            System.out.println("  --------");
            System.out.println(" |        |");
            System.out.println(" 0        |");
            System.out.println("          |");
            System.out.println("          |");
            System.out.println("          |");
        }
        else if (wrong == 2) {
            System.out.println("  --------");
            System.out.println(" |        |");
            System.out.println(" 0        |");
            System.out.println(" |        |");
            System.out.println("          |");
            System.out.println("          |");
        }
        else if (wrong == 3) {
            System.out.println("  --------");
            System.out.println(" |        |");
            System.out.println(" 0        |");
            System.out.println("/|        |");
            System.out.println("          |");
            System.out.println("          |");
        }
        else if (wrong == 4){
            System.out.println("  ---------");
            System.out.println(" |         |");
            System.out.println(" 0         |");
            System.out.println("/|\\       |");
            System.out.println("           |");
            System.out.println("           |");
        }
        else if (wrong == 5){
            System.out.println("  ---------");
            System.out.println(" |         |");
            System.out.println(" 0         |");
            System.out.println("/|\\       |");
            System.out.println("/          |");
            System.out.println("           |");
        }
        else {

            System.out.println("  ---------");
            System.out.println(" |         |");
            System.out.println(" 0         |");
            System.out.println("/|\\       |");
            System.out.println("/ \\       |");
            System.out.println("           |");
            System.out.println( "You lost! Better luck next time!!" );
        }

    }
    // keep track of wrong guesses
    public static String readwords() throws FileNotFoundException {  
        Scanner input = new Scanner(new File("hangman.txt"));   
        String[] word = new String[10];  
        String rword = "";  

        for(int i = 0; i < word.length; i++){  
            if (input.hasNext())  {
                
                word[i] = input.next();    

            }else {
                break;  
                    
            }
        }        

        Random rand = new Random();  

        int r = rand.nextInt(9);  

        rword = word[r];  
        return rword;  
    } 

    public static void userGuess(String y, String[] m, String[] words2){
        for (int i=0; i < y.length(); i++){
            m[i] = "-";
        }
        for (int x = 0; x < y.length(); x++){
            if (x + 1 < y.length())
                words2[x] = y.substring(x, x + 1);
            else
                words2[x] = y.substring(x, y.length());
        }
    }

    public static boolean add(String[] m, String[] ma) {  //find if letters are in the word
        Scanner console = new Scanner(System.in);
        String letters = console.next();
        boolean found = false;

        for(int i = 0; i < ma.length; i++) {
            if(ma [i].equals(letters)){
                m[i] = letters;
                found = true;
            }
        }
        if(found) {
            return true;
        }
        else {
            return false;
        }
    }

    public static void userGuess(String[] m) {
        for(int i = 0; i < m.length; i++) { 
            System.out.print(m[i] );
        }

    }
 
}

/*  public static void playAgainMethod() {
Scanner console = new Scanner(System.in);
int response = console.nextInt();
while (response != 2) {  

//your game code   

while (response != 1 && response != 2)  {  

System.out.println("Make a selection");  
System.out.println("============");  
System.out.println("1. Play again");
System.out.println("2. Quit"); 
System.out.println("============"); 

if (response < 1 || response > 2) {  
System.out.println("Please choose either 1 or 2");       
 */      






ive fixed your if statement you closed it before the else ran so it would never get to true


Wow thanks a lot! Just 2 more things that if you could help me with, it'd be great. I need to get it to say what letters have already been guessed. And I need it to cut the game off when the user wins. Oh and on elast little thing. For some reason, it is printing the correct letters on the hangman dashes at the top of the hangman.
Was This Post Helpful? 0
  • +
  • -

#13 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2198
  • View blog
  • Posts: 5,226
  • Joined: 10-September 10

Re: java.lang.ArrayIndexOutOfBoundsException: 10 error hangman

Posted 05 May 2013 - 02:19 AM

Quote

I need to get it to say what letters have already been guessed.

Keep an array or ArrayList of letters that have been guessed print that at each turn.

Quote

And I need it to cut the game off when the user wins.

This was discussed in posts #9 and #10. What changes did you make based on Gungnir's suggestion?
Was This Post Helpful? 0
  • +
  • -

#14 Tigers27  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 56
  • Joined: 25-March 13

Re: java.lang.ArrayIndexOutOfBoundsException: 10 error hangman

Posted 05 May 2013 - 08:47 AM

Here is the updated code!

import java.io.*; //for file
import java.util.*; //for scanner

public class Lab65 {
    static String userG = "";
    public static void main(String[] args)throws FileNotFoundException{

        int wrong = 0; // keeps track of user guesses
        String word = readwords();
        boolean temp = false;

        String[] words = new String[word.length()];
        String[] words2 = new String[word.length()];

        //methods to be used
        userGuess(word, words, words2);
        printhm(wrong);

        //introduce user to game
        System.out.println("Welcome to hangman!");
        System.out.println("Begin guessing letters!");
        System.out.println("These letters have already been guessed!");
        userGuess(words);

        //keep track of wrong user guesses  

        while(wrong <6) {   
            temp = add(words, words2);
            userGuess(words);

            if(temp == false) { 
                wrong++;
                printhm(wrong);
                System.out.println("Incorrect guess!" + (6 - wrong) + " more guesses left!");
                System.out.println();
                System.out.println(userG);
                
                System.out.println();
                System.out.println(" These letters have already been guessed!");
                System.out.println("Make another guess!");

            }else{    //user guess is correct
                printhm(wrong);
                System.out.println("Correct guess!");
                System.out.println(" These letters have already been guessed!");

                System.out.println(userG);
                System.out.println("Make another guess!");

            }
        }
    }
    //display hangman
    public static void printhm(int wrong) {

        //hangman display 
        if(wrong == 0) {
            System.out.println("  ------");
            System.out.println(" |      |");
            System.out.println("        |");
            System.out.println("        |");
            System.out.println("        |");
            System.out.println("        |");
        }
        else if (wrong == 1){
            System.out.println("  --------");
            System.out.println(" |        |");
            System.out.println(" 0        |");
            System.out.println("          |");
            System.out.println("          |");
            System.out.println("          |");
        }
        else if (wrong == 2) {
            System.out.println("  --------");
            System.out.println(" |        |");
            System.out.println(" 0        |");
            System.out.println(" |        |");
            System.out.println("          |");
            System.out.println("          |");
        }
        else if (wrong == 3) {
            System.out.println("  --------");
            System.out.println(" |        |");
            System.out.println(" 0        |");
            System.out.println("/|        |");
            System.out.println("          |");
            System.out.println("          |");
        }
        else if (wrong == 4){
            System.out.println("  ---------");
            System.out.println(" |         |");
            System.out.println(" 0         |");
            System.out.println("/|\\       |");
            System.out.println("           |");
            System.out.println("           |");
        }
        else if (wrong == 5){
            System.out.println("  ---------");
            System.out.println(" |         |");
            System.out.println(" 0         |");
            System.out.println("/|\\       |");
            System.out.println("/          |");
            System.out.println("           |");
        }
        else {

            System.out.println("  ---------");
            System.out.println(" |         |");
            System.out.println(" 0         |");
            System.out.println("/|\\       |");
            System.out.println("/ \\       |");
            System.out.println("           |");
            System.out.println( "You lost! Better luck next time!!" );
        }

    }
    // keep track of wrong guesses
    public static String readwords() throws FileNotFoundException {  
        Scanner input = new Scanner(new File("hangman.txt"));   
        String[] word = new String[10];  
        String rword = "";  

        for(int i = 0; i < word.length; i++){  
            if (input.hasNext())  {
                
                word[i] = input.next();    

            }else {
                break;  
                    
            }
        }        

        Random rand = new Random();  

        int r = rand.nextInt(9);  

        rword = word[r];  
        return rword;  
    } 

    public static void userGuess(String y, String[] m, String[] words2){
        for (int i=0; i < y.length(); i++){
            m[i] = "-";
        }
        for (int x = 0; x < y.length(); x++){
            if (x + 1 < y.length())
                words2[x] = y.substring(x, x + 1);
            else
                words2[x] = y.substring(x, y.length());
        }
    }

    public static boolean add(String[] m, String[] ma) {  //find if letters are in the word
        Scanner console = new Scanner(System.in);
        String letters = console.next();
        boolean found = false;

        for(int i = 0; i < ma.length; i++) {
            if(ma [i].equals(letters)){
                m[i] = letters;
                found = true;
            }
        }
        if(found) {
            return true;
        }
        else {
            return false;
        }
    }

    public static void userGuess(String[] m) {
        for(int i = 0; i < m.length; i++) { 
            System.out.print(m[i] );
        }

    }
 
}





View PostGregBrannon, on 05 May 2013 - 02:19 AM, said:

Quote

I need to get it to say what letters have already been guessed.

Keep an array or ArrayList of letters that have been guessed print that at each turn.

Quote

And I need it to cut the game off when the user wins.

This was discussed in posts #9 and #10. What changes did you make based on Gungnir's suggestion?

Was This Post Helpful? 0
  • +
  • -

#15 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2198
  • View blog
  • Posts: 5,226
  • Joined: 10-September 10

Re: java.lang.ArrayIndexOutOfBoundsException: 10 error hangman

Posted 05 May 2013 - 09:55 AM

So perhaps the while loop beginning at line 27 should check for more than the number of wrong guesses. You could create another flag, wordGuessed?, that starts out false but goes true in the same while loop if the user guesses the word.

How would you check to see if the word has been guessed?

How would you modify the while loop to check the wordGuessed flag?
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2