So I was reading through David J. Eck's fantastic and free Introduction to Programming Using Java, and he mentioned the .charAt() function within the String class. I got thinking how I could use this in a program to practice an array of techniques. I thought of the idea of making a text based Hangman game. I planned it all out (more or less, I still need to work out how I'll terminate the program once the user has guessed the entire word correct, and how to end it when they've run out of guesses), but I've come into some trouble with using the .charAt() function itself.
I had a similar problem to this before (and the brilliant members of this forum helped me work it out) where I was using '==' when I should have been using .equals(). I tried this in the code below (in the if statement inside the nested for loop) but it just stops the program from running at all.
I feel this is going to be something stupid... well, there is still the possibility that the whole structure of my program is off (it was late when I planned it, xD).
Any help would be very much appreciated, . I'm on my summer holiday now so I'm keen to learn lots and lots of Java!
This was how I tried it with .equals():
if (guess.charAt(0).equals(actualWord.charAt(counter))){ isItCorrect = true; }else{};
This is the rest of the code, with '==' being used:
import java.util.Scanner; class HangmanGame { public static void main(String[] args) { Scanner input = new Scanner(System.in); String actualWord; //The word to be guessed. String guess; //Hold the single letter guess. boolean isItCorrect = false; // To determine whether the user should have the guess counted as failed, thus losing them a guess. int counter; int i; //actualWord is given a value. //Messages are displayed to the user. System.out.println("Please enter the word you would like the other player to guess: "); actualWord = input.nextLine(); System.out.println("Next player, guess the letters. The word contains " + actualWord.length() + " letters."); //1st for loop = the "Guesses Loop": gets the result of isItCorrect from its nested for loop and then sorts out how many guesses are left. //2nd for loop = does the actual comparing of guess to actualWord, cycling through each letter with charAt(counter). for(i = 0; i <=10; i++){ isItCorrect = false; //reseting isItCorrect at the start of each guess, so that a 'Correct!' does not carry over to the next guess. System.out.println("Please guess a letter: "); guess = input.nextLine(); for(counter = 0; counter <= actualWord.length(); counter++){ if (guess.charAt(0) == actualWord.charAt(counter)){ isItCorrect = true; }else{}; } if(isItCorrect = true){ System.out.println("Correct! Letter " + guess + "found at index no. I DONT KNOW!" /*+ SOMEHOWGETTHEINDEXOFTHELETTER)*/); counter = counter - 1; }else{System.out.println("Incorrect. Attempts remaining: " + (10 - counter));} }//end of 1st for loop; "Guesses Loop". } }
Output:
Please enter the word you would like the other player to guess: cat Next player, guess the letters. The word contains 3 letters. Please guess a letter: c Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 3 at java.lang.String.charAt(String.java:686) at HangmanGame.main(HangmanGame.java:29)