0

I get a StringIndexOutOfBounds error with this Java program on the line:

String num3 = lottoString.substring(2,2);

Telling me that 2 is out of the range, but this code should randomly pick a three digit lottery number ranging from 000 through 999. What is my error?

import java.util.Scanner;
public class Lottery
{
    public static void main(String[] args)
    {
        //Declare and initialize variables and objects
        Scanner input = new Scanner(System.in);
        String lottoString = "";

        //Generate a 3-digit "lottery" number composed of random numbers
        //Simulate a lottery by drawing one number at a time and 
        //concatenating it to the string
        //Identify the repeated steps and use a for loop structure
        for(int randomGen=0; randomGen < 3; randomGen++){
            int lotNums = (int)(Math.random()*10);
            lottoString = Integer.toString(lotNums);
        }

        String num1 = lottoString.substring(0,0);
        String num2 = lottoString.substring(1,1);
        String num3 = lottoString.substring(2,2);

        String num12 = num1 + num2;
        String num23 = num2 + num3;
        String num123 = num1 + num2 + num3;

        //Input: Ask user to guess 3 digit number
        System.out.println("Please enter your three numbers (e.g. 123): ");
        String userGuess = input.next();

        //Compare the user's guess to the lottery number and report results
        if(userGuess.equals(num123)){
            System.out.println("Winner: " + num123);
            System.out.println("Congratulations, both pairs matched!");
        }else if(userGuess.substring(0,2).equals(num12)){
            System.out.println("Winner: " + num123);
            System.out.println("Congratulations, the front pair matched!");
        }else if(userGuess.substring(1,3).equals(num23)){
            System.out.println("Winner: " + num123);
            System.out.println("Congratulations, the end pair matched!");
        }else{
            System.out.println("Winner: " + num123);
            System.out.println("Sorry, no matches! You only had one chance out of 100 to win anyway.");
        }
    }
}
1
  • Add a line to print out the length of the string just before you do the substring op. Commented Aug 31, 2014 at 18:49

2 Answers 2

1

As mentioned in the other answer, every time you iterate over your loop, you reset the value of lottoString to just be one digit. You need to append to it, like this:

lottoString += Integer.toString(lotNums);

Your other problem is your use of the substring method. If both index positions are the same, such as 0,0, it returns an empty String. What you want is this:

String num1 = lottoString.substring(0,1);
String num2 = lottoString.substring(1,2);
String num3 = lottoString.substring(2,3);
Sign up to request clarification or add additional context in comments.

2 Comments

Oh, thank you! I thought 3 wouldn't work since there's only 3 digits and the counting is weird with them
@user2337438 Yup, in the official documentation it says: The substring begins at the specified beginIndex and extends to the character at index endIndex - 1.
0
for(int randomGen=0; randomGen < 3; randomGen++){
    int lotNums = (int)(Math.random()*10);
    lottoString = Integer.toString(lotNums);
}

You're assignining the result of Integer.toString() to lottoString. lotNums is a number between 0 and 9.

I guess you want

lottoString += Integer.toString(lotNums);

Comments

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.