Ok trying to get a user input after asking if they want to play again. but for some reason when the end of the game is reached it asks for the input but before the input is able to be given i get an error message reading: "Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 0
at java.lang.String.charAt(String.java:686)
at w004nrl.Game.main(Game.java:87)
Java Result: 1"
What am i doing wrong here? i don't see the problem. maybe i'm too wrapped up in it.
import java.util.*; public class Game { public static void main(String[] args) { Scanner keyboard = new Scanner(System.in); char guess = 0, play = 'y', y = 0; String answer, aaa; int letter, count, computer = 0, player = 0; System.out.println("The purpose of this program is to play a guessing " + "game. \n In the game the computer picks a random capital " + "letter (A - Z) and the player is given up to four tries to " + "guess the letter. \n The player may input either a lower or " + "a upper case letter. \n Once a game is over the program " + "asks if additional games are to be played. \n When no more " + "games are to be played a total score of wins for the " + "computer and the player is displayed."); while (play == 'y') { letter = (int) (Math.random() * 25) + 65; char letter2 = (char) letter; System.out.println(""); //this is to gain a space between lines System.out.println("The game has begun. The computer has chosen" + " a letter.\n"); for (count = 1; count <= 4; ++count) { if (count == 1) { System.out.println("You have 4 guesses to pick the letter," + " enter your guess: "); answer = keyboard.next(); aaa = answer.toUpperCase(); guess = aaa.charAt(0); } else if (count == 2) { System.out.println("You have 3 guesses to the pick the " + "letter, enter your guess: "); answer = keyboard.next(); aaa = answer.toUpperCase(); guess = aaa.charAt(0); } else if (count == 3) { System.out.println("You have 2 guesses to pick the" + " letter, enter your guess: "); answer = keyboard.next(); aaa = answer.toUpperCase(); guess = aaa.charAt(0); } else if (count == 4) { System.out.println("You have 1 guess to pick the " + "letter, enter your guess: "); answer = keyboard.next(); aaa = answer.toUpperCase(); guess = aaa.charAt(0); } if (guess == letter2 && count < 4) { System.out.println("Congratulations you win!"); count = count + 4; player = player + 1; } else if (guess > letter2 && count < 4) { System.out.println("Your guess is alphabetically after the " + "computers generated letter, try again."); } else if (guess < letter2 && count < 4) { System.out.println("Your guess is alphabetically before the " + "computer generated letter, try again."); } else if (guess == letter2 && count == 4) { System.out.println("Congratulations you guessed it!"); player = player + 1; } else { System.out.println("Too bad! The letter was : " + letter2); computer = computer + 1; } } System.out.println("More games? Enter Y if yes " + "and any other character if no: "); play = keyboard.nextLine().charAt(0); } System.out.println("Thank you for playing!"); if (computer == 0 && player > 1) { System.out.println("Game tally is: " + computer + " wins and " + "the player" + player + " wins."); } else if (computer > 1 && player == 0) { System.out.println("Game tally is: " + computer + " wins and the player" + player + " wins."); } else if (computer < 2 && player > 2) { System.out.println("Game tally is: Computer " + computer + " win "); System.out.println("The player " + player + " wins."); } else if (computer > 2 && player > 2) { System.out.println("Game tally is: Computer " + computer + " wins" + "and the player " + player + " wins."); } else if (computer > 2 && player < 2) { System.out.println("Game tally is: Computers " + computer + " wins and the player " + player + " win"); } } }