-1

I am trying to make a program that does cellular automata in 1D. For that, i need to read three variables from a single line. one of the variables, "L", determines the array length of "currentGeneration". However I get the ArrayIndexOut... error. I think this has to do with the dimension of my array and the variable L.

public class Cellulitissss {
    int L;
    Scanner sc = new Scanner(System.in);
    Boolean[] currentGeneration;
    String automaton;
    int G;
    String X;
    String Z;

    public void readGeneral() {

        String[] values = new String[2];
        for (int i = 0; i < 3; i++) {
            values[i] = sc.next();
        }
        automaton = values[0];
        X = values[1];
        Z = values[2];
        L = Integer.parseInt(X);
        G = Integer.parseInt(Z);
        currentGeneration = new Boolean[L + 1];
    }
}
Juan Carlos Mendoza
  • 5,736
  • 7
  • 25
  • 50
Amaan V
  • 3
  • 1
  • 2
  • 1
    Because `values[i]` exists only for `i = 0,1` (size =2) – Naman Sep 25 '17 at 15:10
  • There is no index `2`, your array has indexes `0` and `1` . – Arnaud Sep 25 '17 at 15:10
  • And look into your naming: names such as XLZG mean **nothing**. There is absolutely no good reason to use a single uppercase character as names for *anything*. Thus: read about java naming conventions, and start practicing that. – GhostCat Sep 25 '17 at 15:12
  • L and G was given for the assignment, X and Z are definitely unnecessary. thanks for the feedback. btw also SOLVED. Thanks all ! – Amaan V Sep 25 '17 at 15:40

1 Answers1

0

You have tried to access values [2] which is wrong because array index starts from 0 so an array storing 2 values will store them at locations a[0] and a[1] if a is your array name. So trying to access a[2] will give an array index out of bounds exception

Shivam...
  • 409
  • 1
  • 8
  • 21