0

Seems like my main variables n and m cant be changed via method dimensions. Console says that there is a problem at method create in this line a [ i ][ j ] = unos.nextInt(); but if i change this line private int[ ][ ] a = new int[n][m]; and put any number like [3][4], the program works, but with [n][m] it does not, can u help me guys, whats wrong with this code. CONSOLE: a[1][1]=Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0 Thanks in advance..

import java.util.Scanner;

public class Matrica {
private int n, m;
private Scanner unos = new Scanner(System.in);

public void dimensions() {
    System.out.print("n: ");
    n = unos.nextInt();
    System.out.print("m: ");
    m = unos.nextInt();

}

private int[][] a = new int[n][m]; // if i put [2][2] or any other number, instead [n][n], program works

public void create() {
    for (int i = 0; i < n; i++)
        for (int j = 0; j < m; j++) {
            System.out.print("a[" + (i + 1) + "][" + (j + 1) + "]=");
            a[i][j] = unos.nextInt(); // console points that this is the problem
        }
}

public void print() {
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < m; j++) {
            System.out.printf("%d\t", a[i][j]);
        }
        System.out.println();
    }
}
}
2
  • 1
    @Thrasher why did you edit this to add all the unneeded spaces? This is not typical Java style and just looks weird. Commented Dec 18, 2016 at 5:33
  • 1
    @Thrasher I don't recommend doing this in the future. Your personal style is your business, but there are some fairly well-established standards for Java spacing (not all of which I like, but I live with them), and I think it's best to work within that on a site with tons of readers. Commented Dec 18, 2016 at 5:41

2 Answers 2

1

The problem is that

private int[][] a = new int[n][m]; 

is being executed before the code in the constructor is executed. That is, the new is being done when n and m haven't been set, and at this point they've been initialized to 0 by default. So it's allocating an array with no rows or columns.

To fix this, change the above to

private int[][] a;

and initialize it in the constructor, after n and m have been set:

a = new int[n][m];

For more information on the order in which things are executed when an instance is created, see this section of the JLS.

Sign up to request clarification or add additional context in comments.

Comments

0

Like @ajb said, initialize the array after the variables n & m have obtained there values using the Scanner. You can do this in the dimensions() method.

public void dimensions() {
    System.out.print("n: ");
    n = unos.nextInt();
    System.out.print("m: ");
    m = unos.nextInt();
    a = new int[n][m]; //Add the following line.
}

2 Comments

hmm, i thought its all about the order, didnt know about that kind of priority.. learned something new
It is not always about the order in which you write your code. Java is a fully object oriented language and when you run the code, no matter where you have created your class level variables (whether they are the first few lines of code in your class or the last) the variables will be created before the actual code execution takes place.

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.