1

here is my program

import java.util.Scanner;
import java.util.Random;

public class Project3
{
    public static void main(String[] args)
    {

        int low;
        int high;
        int answer;
        int guess;
        int numGuess = 0;
        int x;
        int y;

       char repeat; // this will hold y or n 
       String input; //holds input to perform entire loop


        System.out.println( "Hello and welcome to Guess That Number!");

            Scanner keyboard = new Scanner(System.in);

        System.out.println("For starters you get to pick the range the number falls in!" +
                            " HOW EXCITING!");


        System.out.println( "Now what would you like the lowest possible number to be?");
                      low = keyboard.nextInt();

        System.out.println( "and the highest?");
                      high = keyboard.nextInt();
 do  
  {    Random randomNumber = new Random();

        answer = randomNumber.nextInt();
       while (answer < low || answer > high)
       { 
        answer = randomNumber.nextInt();
    }



       guess = -1;

       while(guess != answer)
       {

           System.out.println("What is your guess?");
          System.out.println("Don't forget has to be in between "+ low + " and " + high);

                   guess = keyboard.nextInt();

                   numGuess = (numGuess + 1);

                if (guess < answer)
                {
                    System.out.println("TOO LOW!");

                }

                else if (guess > answer)
                {
                    System.out.println("TOO HIGH!");



                }


            }





       System.out.println("YOU GOT IT WOOOO!");
       System.out.println("The number was " + answer);
       System.out.println("Nice it only took " + numGuess + "!");

       for ( x = 1; x <= numGuess; x++)

      {

          for ( y = 1; y <= answer; y++)

            {
                System.out.print("*");
            }

            System.out.println();



      }


      System.out.println("\nWould you like to play again? \n" +        // this is to loop the entire game
                      "Enter Y for yes or N for no. \n");

                      input = keyboard.nextLine();
                      repeat = input.charAt(0);

  } while (repeat == 'Y' || repeat == 'y');

    if (repeat == 'n' || repeat == 'N')             
    {

    System.out.println("\nThanks for playing! \n");

   }
}
}

when i try and run it it gives me this error message

java.lang.StringIndexOutOfBoundsException: String index out of range: 0 at java.lang.String.charAt(String.java:687)

how do i fix it so the program loops correctly?!?

1
  • He'll get a mark for his source code at school and he has too little time until deadline. That's why he didn't write "I urgently need to get rid of this error", but "urgent java error". Of course, this is not a Java error either, it's the error of a novice programmer. Commented Oct 28, 2010 at 3:40

5 Answers 5

3

before you do this: repeat = input.charAt(0);

check if the string has at-least one character.

Sign up to request clarification or add additional context in comments.

2 Comments

Not related to the question exactly, but if you enter anything but y or n, the program will just exit without saying anything.
agreed but before the control can reach the last statement of the program, the code "repeat = input.charAt(0);" is always executed. hence the error. therefore my suggestion above!
2

It seems that the user is pressing return/enter without inputing Y or N.

Here is the suggestion. No doubt that the code can be made better in many ways, but this is just a suggestion for this very issue. Get rid of the repeat variable completely, and replace respective lines with these.

do {
    .....

} while ("y".equalsIgnoreCase(input));

if (!"y".equalsIgnoreCase(input)) {
  System.out.println("\nThanks for playing! \n");
}

Comments

2

First, just a side note, as mootinator stated, your while condition will be false if any input is not 'Y' or 'y', thus exit and the final "\nThanks for playing! \n" will never be displayed. You should revise this construct. Perhaps something like :

boolean playing = true;

while (playing) {
   // play game here

   // ask to play again?

   if (answer == 'N') {
      playing = false;
   }
}

System.out.println("\nThank you for playing!\n");

Now, to address your original problem, you are not checking for empty input. The error is very likely to happen only when you hit enter without entering anything. Another question is then, what to do with empty values? Is it considered to be 'N' or 'Y'? If you consider empty value to be a valid default choice, you should indicate it in your displayed question. Something like:

System.out.print("\nWould you like to play again?\n" + 
                 "Enter Yes or No (default Yes) : ");
do {
   input = keyboard.nextLine().toUpperCase();  // 'Y' == 'y';
   if (input.length() == 0) {  // if the input is empty, we default the value
      repeat = 'Y';     // default value, change this to 'N' if default is No
   } else {
      repeat = input.charAt(0);
      if (repeat != 'N' && repeat != 'Y') {
         System.out.print("Ooops! Please enter Yes or No :");
         repeat = '\0';
      }
   }
} while (repeat == '\0');
// At this point, repeat is either 'Y' or 'N' only, so no need to check for lowercase

If you don't want a default value, simply change the construct to

System.out.print("\nWould you like to play again?\n" + 
                 "Enter Yes or No : ");
do {
   input = keyboard.nextLine().toUpperCase();  // 'Y' == 'y';
   if (input.length() == 0) {  // if the input is empty...
      repeat = '\0';     // null character for empty value
   } else {
      repeat = input.charAt(0);
   }
   if (repeat != 'N' && repeat != 'Y') {
      System.out.print("Ooops! Please enter Yes or No :");
      repeat = '\0';     // make sure the character is null so we don't exit the loop yet
   }
} while (repeat == '\0');
// At this point, repeat is either 'Y' or 'N' only, so no need to check for lowercase

Comments

0

IndexOutOfBoundsException means the index you're trying to access of the array does not exist. The exception shows you it's encountering the error at line 687 and it's the String.charAt() method. I'd recommend by taking a closer look at your code around that line. If you're using an IDE, you should try executing in debug with a breakpoint near that line and stepping through the code line by line to watch the variables.

Comments

0

Instead of this:

repeat = input.charAt(0);

Do this:

try
{
    repeat = input.charAt(0);
}
catch (java.lang.StringIndexOutOfBoundsException exception)
{
//Solve the problem
}

This way you can catch your exception and handle it. EDIT: It's better to learn a little Exception handling in Java in your first lesson, because it's not complicated and will be useful at later, more difficult tasks.

11 Comments

I would really appreciate if I knew what's wrong in my answer from the downvoter's point of view.
Not sure, but it seems that this is because you are suggesting to catch a RuntimeException, which is not really a good practice. However, that indeed can fix the problem in hand. But its still a bad idea, especially in this case. The question is why let the exception occur, when you can avoid that altogether in the first place.
We can't solve the problem as he wants, because he didn't specify how he wants to solve the problem, so we need a general solution prototype, which can either be preventing the exception from occuring or catching it. I disagree with the statement that catching runtime exceptions is a bad practice, because in many cases it's the only solution and, the student who asked the question should learn about exception handling too.
I just want it to loop the program back to picking a new number then having the user guess it again with the max and min still preset. the way it is written should work I did what they showed to do in this java textbook and I've already used this format in a different program and it runs fine. I just didn't know why this time around it's not working.
@Lajos Arpad: Show me the example where catching RuntimeException is the only solution. I mean, c'mon, learn to appreciate people advices. Remember, the chap gave you negative and didn't bother to make any comments. Here I am suggesting you the possible reason for that -ve, and you are denying it. First you stated that the questioner never mentioned how he want to solve the problem, then you are suggesting him to learn Exception Handling. Where did he stated he want to learn Exception Handling, BTW? My suggestion to you is, stop arguing irrationally and try to learn from your mistakes.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.