I'm doing a course of java and I tried to do this exercise without help, but my cdm says to me:
[1][2][3]Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 3 at Matrices.main(Matrices.java:19) Even so, I copied the exercise of my teacher and he didn't have any mistake. Help me please. Thanks.
My exercise:
import java.util.Scanner;
public class Matrices{
public static void main(String args[]){
int contador = 1, filas = 0, columnas = 0;
Scanner entrada = new Scanner(System.in);
System.out.println("¿Cuantas filas quiere?");
filas = entrada.nextInt();
System.out.println("¿Cuantas columnas quiere?");
columnas = entrada.nextInt();
int matriz1 [][] = new int [filas][columnas];
for(int j = 0; j < filas; j++){
for(int i = 0; i < columnas; j++){
matriz1[j][i] = contador;
contador++;
System.out.print("[" + matriz1[j][i] + "]");
}
System.out.println("");
}
}
}
My teacher exercise:
import java.util.Scanner;
public class MatricesDinamicas{
public static void main(String args[]){
int filas = 0, columnas = 0, contador = 1;
Scanner entrada = new Scanner(System.in);
System.out.println("¿Cuantas filas deseas?");
filas = entrada.nextInt();
System.out.println("¿Cuantas columnas deseas?");
columnas = entrada.nextInt();
int numeros [][] = new int [filas][columnas];
for(int j = 0; j < filas; j++){
for(int i = 0; i < columnas; i++){
numeros[j][i] = contador;
contador++;
System.out.print("[" + numeros[j][i] + "]");
}
System.out.println("");
}
}
}