0

So this is his code:

public void parseAll() {
    String[] opis = new String[this.d];
    Integer[] velikost = new Integer[this.d];
    Integer[] poraba = new Integer[this.d];
    Trio singleResult;

    for (int i = 0; i < this.d; i++) {
        singleResult = parseLine(this.dekl[i]);
        opis[i] = singleResult.enOpis;
        velikost[i] = singleResult.enaVelikost;
        poraba[i] = singleResult.enaPoraba;
    }

    this.opis = opis;
    this.velikost = velikost;
    this.poraba = poraba;
}

public Trio parseLine(String line) {
    Trio ret = new Trio();
    Trio oth;
    String[] token = line.split("\\s+");

    if ("prim".equals(token[0])) {
        ret.enaVelikost = new Integer(token[1]);
        ret.enOpis = "prim" + ret.enaVelikost.toString();
        ret.enaPoraba = (ret.enaVelikost.intValue() / this.b) * this.b;
        if (ret.enaVelikost.intValue() % this.b > 0)
            ret.enaPoraba = new Integer(ret.enaPoraba.intValue() + this.b);
        ret.tokensUsed = new Integer(2);
    } else { // the rest of the code not in question

It says the error is in the following lines:

ret.enaVelikost = new Integer(token[1]);
singleResult = parseLine(this.dekl[i]);

Now he went out of his way for this and i am having difficulty reading and understanding the problem. This is a program for translating into a made-up programming language and the error happens when i try to input the following:

8 // this number does nothing in this part of the program
1 // this is a number of lines with declaration of data types
prim 3 // prim stands for primitive type and the 3 is the size of it
3 // this here is the number of commands i want to run
1 1 // these are the commands, the first number is the command number and the second one is for the line i want to run the command in
2 1 
3 1

The input works until i hit enter after the second command (2 1), then the error appears. this is the exact error message : java.lang.ArrayIndexOutOfBoundsException: Index 1 out of bounds for length 1 I honestly have no clue as to why it would give an error. Any help is apreciated.

simsam
  • 35
  • 5
  • use a debugger to inspect the state of your fields as the program runs – Hovercraft Full Of Eels Jan 05 '20 at 16:31
  • You're going to need to post the full code more than likely as there may be more context with regards to the use of dekl array. If the error is happening on `ret.enaVelikost = new Integer(token[1]);` then it's quite likely that line.split("\\s+") is returning an empty array OR an array with length 1, in which case trying to access element 1 in the array will cause an ArrayIndexOutOfBoundsException because arrays are 0 indexed and therefore a length of 1 means the element is at index 0, and a length of 0 means the array is empty and you cannot access it at all. Check the length before reading. – Thomas Cook Jan 05 '20 at 16:34

0 Answers0