Mike
Mike

Reputation: 11

Can't find what's wrong with code. ArrayIndexOutOfBoundsException

So, I have this homework, the code does what It's supposed to, but at some cases the program crashes. I've been going trough it for hours and I just cant find a way to fix it. I have sort of a clue what's wrong but I can't fix it. Just don't know how. The problem seems to be with the y,z integers or the soucetSloupce soucetRadku methods, don't know where exactly the problem is.

package mazorku5;
import java.util.Arrays;
import java.util.Random;
import java.util.Scanner;

public class MazorKU5 {
    public static int[][] dejPole(int b, int a){
    int[][] pole = new int[b][a];

    for (int i = 0; i < pole.length; i++){
        for (int j = 0; j < pole[i].length;j++){
            pole[i][j] = (int)(Math.random()*10);
        }
    }
    return pole;
}

public static void tiskPole(int [][] pole){
    for (int i = 0; i<pole.length; i++){
        for (int j = 0; j < pole[i].length;j++) {
            System.out.print (pole[i][j] + " ");
        }
    System.out.println(); 
    }
}

public static int soucetRadku(int[][]pole, int z){
    int suma=0;
    for (int j = 0; j < pole[z].length;j++){
        suma = suma + pole[z][j];
    }
    return suma;
}

public static int soucetSloupce(int[][]pole, int y){
    int suma=0;
    for (int j = 0; j < pole[y].length;j++){
        suma = suma + pole[j][y];
    }
    return suma;
}

public static float aritmPrumer(int[][]pole){
float suma=0;
    for (int i = 0; i<pole.length; i++){
        for (int j = 0; j < pole[i].length;j++){
            suma = suma + pole[i][j];
        }
    }
    return suma/(pole.length*pole[0].length);
}

public static int kolikX(int[][]pole, int x){
    int pocet=0;
    for (int i = 0; i<pole.length; i++){
        for (int j = 0; j < pole[i].length;j++){
            if (pole[i][j] == x)pocet++;
        }
    }
    return pocet;
}

public static int loser(int[][]pole, float prumer){
    int pocet=0;
    for (int i = 0; i<pole.length; i++){
        for (int j = 0; j < pole[i].length;j++){
            if (pole[i][j] < prumer)pocet++;
        }
    }
    return pocet;
}

public static void main(String[] args) {
    Random ran = new Random();
    Scanner sc = new Scanner(System.in);
    int i = 0;
    int znovu = 1;
    int z; 
    int y;
    int x;
    int loserP;
    float prumer;
    while (znovu==1) {
        System.out.println("Zadejte kolik ma mit pole sloupcu.");
        int a=sc.nextInt();
        while (a <= 0) {
            System.out.println("Zadejte kolik ma mit pole sloupcu.");
            a= sc.nextInt();
        }
        System.out.println("Zadejte kolik ma mit pole radku.");
        int b=sc.nextInt();
        while (b <= 0) {
            System.out.println("Zadejte kolik ma mit pole radku.");
            b= sc.nextInt();
        }
        int [][] pole = new int[b][a];
        pole= dejPole(b,a);
        tiskPole(pole);
        System.out.println("Radek?");
        z= sc.nextInt()-1;
        while (z > b || z < 0) {
            System.out.println("Radek?");
            z= sc.nextInt()-1;
        }
        System.out.println(soucetRadku(pole,z));
        System.out.println("Sloupec?");
        y= sc.nextInt()-1;
        while (y > a || y < 0) {
            System.out.println("Sloupec?");
            y= sc.nextInt()-1;
        }   
        System.out.println(soucetSloupce(pole,y));
        prumer=aritmPrumer(pole);
        System.out.println("Jake cislo hledame?");
        x = sc.nextInt();
        int pocetX=kolikX(pole,x);
        System.out.println(x + " mame: " + pocetX + ". ");
        System.out.println("Aritm. prumer je: " + prumer);
        loserP=loser(pole, prumer);
        System.out.println("Hodnot mensich nez prumer je: " + loserP);
        System.out.println("Znovu?(1)");
        znovu = sc.nextInt();
    }
}
}

Upvotes: 1

Views: 52

Answers (1)

Jan
Jan

Reputation: 13858

Your indices are off in

public static int soucetSloupce(int[][]pole, int y){
   int suma=0; for (int j = 0; j < pole[y].length;j++){ // pole.length??
    suma = suma + pole[j][y]; //  y,j ??
   }
 }

Upvotes: 2

Related Questions