The problem is at int [][]tam = new int [a][b]. It’s only that line. I am new to Java and coming from a C++ background.
//"Exercitiul" 3
Scanner input = new Scanner(System.in);//instructiune scanner
DataInputStream dis4 = new DataInputStream(System.in);
DataInputStream dis5 = new DataInputStream(System.in);
String st1 = null;
String st2 = null;
try{
System.out.println("Introduceti numarul de Randuri");
st1 = dis4.readLine();
System.out.println("Introduceti numarul de Coloane");
st2 = dis5.readLine();
}
catch (IOException ioe) {
ioe.printStackTrace();
}
int a = Integer.parseInt(st1);
int b = Integer.parseInt(st2);
int [][]tam = new int[a][b];
System.out.println("Verificare " + tam[a][b]);
System.out.println("Introduceti Elementele Matricei ");
for (int m=0 ; m < tam.length ; m++)
for (int n=0 ; n < tam[i].length ; n++){
tam[m][n] = input.nextInt();//instructiune scanner
}
System.out.println("Matricea A: ");
for (int m=0 ; m < tam.length ; m++)
{ System.out.println();
for (int n=0 ; n < tam[i].length ; n++)
System.out.print(tam[m][n]+" ");
}
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 3
at Program.main(Program.java:95)
If an array
ahas lengthn, the elements area[0]througha[n-1].In your case, you allocated
tamas anint[a][b]and the bottom-right element of the matrix istam[a-1][b-1].Also I think you want
mas an index in your inner loopfor (int n=0 ; n < tam[i].length ; n++)instead ofi.