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();
}
}
}