1

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();
    }
}
}
Guy
  • 34,831
  • 9
  • 31
  • 66
Mike
  • 11
  • 2

1 Answers1

2

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 ??
   }
 }
Jan
  • 13,319
  • 3
  • 27
  • 51
  • It actually works for all values except for when y is bigger by exactly 1. No idea why. the code works except for that one value. – Mike Dec 24 '15 at 10:27
  • Whenever your rows are less than your columns I think it will break. – Jan Dec 24 '15 at 10:31
  • but why? And is there anything I can do about that? I do need to have the array customizable. Edit: You are right, so it seems there are two problems, or one that I don't understand at all. – Mike Dec 24 '15 at 10:38
  • Can you explain what that method is supposed to count? – Jan Dec 24 '15 at 10:42
  • Sure, it's supposed to, after getting the array, add numbers in the array in a row that you select and in a column that you select. And some other things but they work nicely. Soucet=sum. Sloupce=column, radku=row. – Mike Dec 24 '15 at 10:56
  • On that case its the loop condition thats off - need to loop until pole.length not pole [y].length – Jan Dec 24 '15 at 11:00
  • Now that's fixed the problem of crashing when column was higher than row, still, the main problem remains. The program crashes when we're looking for the sum of a column or a row that's exactly 1 higher than the maximum. – Mike Dec 24 '15 at 11:13
  • How about an if to check if its outside the boundary? – Jan Dec 24 '15 at 11:43
  • There is the loop that acts if its bigger than 2 and it should theoretically work. It doesn't. The thing is it should work and I have no idea what to do with it. It's in the main. – Mike Dec 24 '15 at 11:46