Raphael Bryan
Raphael Bryan

Reputation: 11

Declaring array size with variable leads to out of bounds exceptiong

I'm creating an int array secretNumber. When I declare the array size as a number, there's no out of bounds exception, but when I declare the array size with a variable (numDigits) I get the out of bounds exception at Index 0 for the line ' secretNumber[i] = val '. This is the class:

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

public class Engine {
    public int numDigits;
    public int[] secretNumber = new int[numDigits]; //this is the array
    public Random randomNumberGenerator;

    public void setNumDigits() {
        Scanner setNumDigits = new Scanner(System.in);
        System.out.println("Enter the number of digits to use");
        String numDigits = setNumDigits.nextLine();
        this.numDigits = Integer.parseInt(numDigits);
    }

    public int getNumDigits() {
        return this.numDigits;
    }

    public void generateNewSecret() {
        int val;
        for (int i = 0; i < numDigits - 1; i++) {
            Random rand = new Random();
            val = rand.nextInt(9);
            secretNumber[i] = val; //out of bounds exception is here
        }
    }

    public void setSecretNumber(int[] secretNumberCopy) {
        secretNumberCopy.equals(this.secretNumber);
    }

    public int[] getSecretNumber() {
        return secretNumber;
}



}

This is the main that executes the methods, and I run the numDigits setter before setting the array:

import java.util.Scanner;

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

        Player playerOne = new Player();

        playerOne.setName();

        System.out.println(playerOne.getName());

        Engine engine = new Engine();

        engine.setNumDigits();
        engine.setSecretNumber(engine.secretNumber);
        engine.generateNewSecret();

        System.out.println(engine.getSecretNumber());


    }
}

Why would Index 0 be out of bounds if I've set numDigits??

Upvotes: 1

Views: 81

Answers (2)

J-Alex
J-Alex

Reputation: 7107

While we have already some clarification to the reason you got the exception there is still a question how to fix it and how to avoid such cases in future.

Let's go through your code step-by-step:

Engine engine = new Engine();

New engine object is created an all class variables instantiated to their default values. Default value for primitive int is 0;

At the moment of initialization you have:

public int numDigits; // 0
public int[] secretNumber = new int[numDigits]; // arrays object of size 0
public Random randomNumberGenerator; // null

How to proceed with that?

The issue is partially in object design - you need to identify the invariants that constrain the state variables. You need to set the size of the array during numDigits initialization:

public int[] secretNumber; // null at the moment of object initialization

public void setNumDigits() {
    Scanner setNumDigits = new Scanner(System.in);
    System.out.println("Enter the number of digits to use");
    numDigits = Integer.parseInt(setNumDigits.nextLine());
    secretNumber = new int[numDigits];
}

Upvotes: 1

user8642594
user8642594

Reputation: 58

As others have commented on you are creating the array with a variable, but that variable is left to the default integer value, which is 0. Instead, don't make the array until you know the size you want. This will fix your issue.

public class Engine {
    public int numDigits;
    public int[] secretNumber; // leave empty
    public Random randomNumberGenerator;

    public void setNumDigits() {
        Scanner setNumDigits = new Scanner(System.in);
        System.out.println("Enter the number of digits to use");
        String numDigits = setNumDigits.nextLine();
        this.numDigits = Integer.parseInt(numDigits);
        secretNumber = new int[this.numDigits]; // build it here
    }
}

As a final note, what happens to your code if I enter a string or a double instead of an integer? It's always a good idea to consider common use cases like that.

Upvotes: 0

Related Questions