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 2 of 2

Thread: Game requesting user input

  1. #1
    Junior Member Neo's Avatar
    Join Date
    Feb 2011
    Location
    Ohio
    Posts
    14
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default 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.


    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");
            }
     
        }
    }


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default 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.

Similar Threads

  1. Replies: 1
    Last Post: November 2nd, 2012, 02:21 PM
  2. Allowing user to input variable.
    By That1guy in forum What's Wrong With My Code?
    Replies: 5
    Last Post: November 15th, 2010, 11:30 PM
  3. Help with toString and accesing user input
    By waltersk20 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 1st, 2010, 07:58 PM
  4. how do I keep a persistent prompt in JTextArea and allow user input
    By cazmo in forum What's Wrong With My Code?
    Replies: 2
    Last Post: February 23rd, 2010, 01:14 PM
  5. User Input Loop
    By cfmonster in forum Loops & Control Statements
    Replies: 7
    Last Post: August 24th, 2009, 01:52 PM