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.

Game requesting user input

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.

importjava.util.*;publicclass Game {publicstaticvoid 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 linesSystem.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);}elseif(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);}elseif(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);}elseif(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;}elseif(guess > letter2 && count <4){System.out.println("Your guess is alphabetically after the "+"computers generated letter, try again.");}elseif(guess < letter2 && count <4){System.out.println("Your guess is alphabetically before the "+"computer generated letter, try again.");}elseif(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.");}elseif(computer >1&& player ==0){System.out.println("Game tally is: "+ computer +" wins and the player"+ player +" wins.");}elseif(computer <2&& player >2){System.out.println("Game tally is: Computer "+ computer +" win ");System.out.println("The player "+ player +" wins.");}elseif(computer >2&& player >2){System.out.println("Game tally is: Computer "+ computer +" wins"+"and the player "+ player +" wins.");}elseif(computer >2&& player <2){System.out.println("Game tally is: Computers "+ computer
+" wins and the player "+ player +" win");}}}

Re: Game requesting user input

Inspect the use of the Scanner....next returns the next token, and thus there may be components still in the buffer of the Scanner which may cause a different use of the scanner methods (such as nextLine()) to return immediately (in this case a new line which then returns an empty string). Hence a charAt on an empty string throws an exception. Quick fix: be consistent in your usage of the scanner methods.