Array exception out of bounds
Code Java:
import java.util.Scanner;
public class masyvai
{
public void masyvas()
{
//sk – 5 , z – 2,4,6,8
Scanner scan2 = new Scanner(System.in);
int a,b;
System.out.printf("Iveskite eiluciu kieki");
a=scan2.nextInt();
System.out.printf("Iveskite stulpeliu kieki");
b=scan2.nextInt();
int mtr[][] = new int[a][b];
System.out.println(" --------------------------------------------------");
System.out.println("| Sumavimas | Atimtis |");
System.out.println(" --------------------------------------------------");
for (int eil = 0; eil < a; eil++)
{
for (int stul = 0; eil < b; stul++)
{
System.out.print(" "+mtr[eil][stul]); // error here, wtf???
}
System.out.print("\n");
}
scan2.close();
}
}
What can be the problem here? I really dont understand, I used this code on my netbook (XP win) and it works fine but when i used it on my pc (win7) it crashes and gives me a gift:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 2
at masyvai.masyvas(masyvai.java:28)
Re: Array exception out of bounds
Why are you comparing eil to both a and b in your inner and outer loop? Is that really what you want to be doing in your inner loop?
Re: Array exception out of bounds
Hello gabberlt. Welcome to the Java Programming Forums.
The java.lang.ArrayIndexOutOfBoundsException means that your Array Index is Out of Bounds ;)
I would look at the for loops. Try using System.out.println(); to see what's going on.