0

This is my first question here. I am trying to write a program that reads as many arrays as I input from the keyboard. After inputting the number of columns I get the error: Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0

Thanks in advance.

package Final;

import java.util.*;

public class Final {
    int a,b,x,l;
    double mat[][][];

    Final()
    {
        a=0;
        b=0;
        c=0;
        d=0;
        x=0;
        l=0;
    }

    public void read() {
        Scanner citast = new Scanner(System.in);
        System.out.println("How many arrays do you want to enter?");
        x=citast.nextInt();
        for (this.l=0; this.l<x; this.l++)
        {
              System.out.println("Matrix "+this.l);
              System.out.println("Number of lines: ");
              this.a = citast.nextInt();
              System.out.println("Number of columns: ");
              this.b = citast.nextInt();
              this.mat= new double [this.l][a][b];
              for (int i=0; i<this.a; i++)
               {
                  for (int j=0; j<this.b; j++)
                   {
                      System.out.print("Matrice " + l + " ["+ i +"]["+ j +"]= ");
                      this.mat[l][i][j]=citast.nextInt();
                   }
                  System.out.println();
            }
        }
    }

    public static void main(String[] args) {
        Final a = new Final();
        Scanner citast = new Scanner(System.in);
        a.read();
        System.out.println();

}

}
5
  • What is the point of having Scanner citast = new Scanner(System.in); in main? Nov 24, 2013 at 15:05
  • 1
    try to give meaningful variables names like rows height columnsIterator...
    – iShaalan
    Nov 24, 2013 at 15:08
  • Do you really want to allow differently sized matrices for each l?
    – Ridcully
    Nov 24, 2013 at 15:10
  • @inixsoftware The code was edited here, I have more functions in the program and I forgot to remove the Scanner. Nov 24, 2013 at 15:38
  • The irony of having a mutable class named Final
    – scottb
    Nov 24, 2013 at 16:15

3 Answers 3

4
this.mat= new double [this.l][a][b];

Should be:

this.mat= new double [x][a][b];

But your code is wrong, you should set the dimension of the array before the first for loop :

public void read() {
        Scanner citast = new Scanner(System.in);
        System.out.println("How many arrays do you want to enter?");
        x=citast.nextInt();
        this.mat = new double[x][][]; //<-- added this line
        for (this.l=0; this.l<x; this.l++)
        {
            System.out.println("Matrix "+this.l);
            System.out.println("Number of lines: ");
            this.a = citast.nextInt();
            System.out.println("Number of columns: ");
            this.b = citast.nextInt();
            this.mat[l] =  new double [a][b]; //<-- modified this line
            for (int i=0; i<this.a; i++)
                for (int j=0; j<this.b; j++)
                {
                    System.out.print("Matrice " + l + " ["+ i +"]["+ j +"]= ");
                    this.mat[l][i][j]=citast.nextInt();
                }
            System.out.println();
        }
        System.out.println(Arrays.deepToString(mat));
    }

Output for an array containing 2 square matrices (one filled with 4 and one with 5):

[[[4.0, 4.0], [4.0, 4.0]], [[5.0, 5.0], [5.0, 5.0]]]
0
1

This should work. At first you create an array of size x to hold arbitrary sized two-dimensional arrays. Then for each l you create a two-dimensional array of size a x b, put it at position l of mat and read the values into it.

Scanner citast = new Scanner(System.in);
System.out.println("How many arrays do you want to enter?");
x=citast.nextInt();

this.mat = new double[x][][];

for (this.l=0; this.l<x; this.l++) {
    System.out.println("Matrix "+this.l);
    System.out.println("Number of lines: ");
    this.a = citast.nextInt();
    System.out.println("Number of columns: ");
    this.b = citast.nextInt();
    this.mat[this.l] = new double [a][b];
    for (int i=0; i<this.a; i++)
        for (int j=0; j<this.b; j++)
        {
            System.out.print("Matrice " + l + " ["+ i +"]["+ j +"]= ");
            this.mat[l][i][j]=citast.nextInt();
        }
        System.out.println();
    }
}
0
0
for (int j=0; j<this.b; j++)
    {
        System.out.print("Matrice " + l + " ["+ i +"]["+ j +"]= ");
        this.mat[l][i][j]=citast.nextInt();
    }

If you leave

this.mat[l][i][j] = citast.nextInt(); 

out of it, then it's working.

Also you have to make Final() static and declare c and d in your class, because they are in the constructor, but there is no reference to them in the class.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.