i got a problem to make a program where i should use arrays

the problem is in the next part, even though all the for loops are the same in this array

results[4][5] = i; // problem here nothing gets stored
nothing gets stored, so when i run the program it doesn't display what it should.

while testing i tried using instead of an array an int
int index = i;
but still it didn't store the value of 'i'
what i don't understand is why in the other arrays the values are stored as it should

i would appreciate some help
thanks

for (int i = 0; i < noise.length - 1; i++) {
                if (results[4][0] < results[2][i]) {
                    results[4][0] = results[2][i]; //this one saves ok
                    results[4][1] = i; //this one saves ok
                }

            }
            for (int i = 0; i < noise.length - 1; i++) {
                if (results[4][2] > results[2][i]) {
                    results[4][2] = results[2][i]; //this one saves ok
                    results[4][3] = i; //this one saves ok
                }
            }
            for (int i = 0; i < noise.length - 1; i++) {

                if (results[4][6] > results[3][i]) {
                    results[4][6] = results[3][i]; //this one saves ok
                    results[4][7] = i;  // problem here nothing gets stored
                }
            }
            for (int i = 0; i < noise.length - 1; i++) {
                if (results[4][4] < results[3][i]) { 
                    results[4][4] = results[3][i]; //this one saves ok
                    results[4][5] = i;  // problem here nothing gets stored
                }

here is all my code

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class AplicacionMatriz3 {

    public static void main(String[] args) throws IOException {
        int noise[][] = { { 20, 30, 40, 50, 60, 70, 80 },
                { 88, 90, 94, 102, 111, 122, 134 },
                { 75, 77, 80, 86, 94, 103, 113 },
                { 80, 83, 85, 94, 100, 111, 121 },
                { 68, 71, 76, 85, 96, 110, 125 },
                { 77, 84, 91, 98, 105, 112, 119 },
                { 81, 85, 90, 96, 102, 109, 120 } };
        String brand[] = { "Speed", "Focus", "Jetta", "Contour", "Tsuru",
                "Explorer", "Lobo" };

        int results[][] = operations(noise);

        int choice = 0;
        boolean quit = false;
        while (!quit) {
            System.out.println("\n\n\n1. Mostrar todos los reportes"
                    + "\n2. Reporte por coche" + "\n3. Reporte por velocidad"
                    + "\n4. Promedio nivel de ruido total" + "\n5. Salir"
                    + "\n   Digite su seleccion: ");
            BufferedReader in = new BufferedReader(new InputStreamReader(
                    System.in));
            choice = Integer.parseInt(in.readLine());
            switch (choice) {
            case 1:
                for (int i = 0; i < noise.length; i++) {
                    System.out.printf("%-10s", brand[i]);
                    for (int j = 0; j < noise.length; j++) {
                        System.out.printf("%4s", noise[j][i]);
                    }
                    System.out.print("\n");
                }

                System.out.print("\nPresione enter para continuar");
                in.readLine();
                break;
            case 2:
                for (int i = 0; i < brand.length - 1; i++) {
                    // -1 por que el 0 en el array no es un coche (velocidad)
                    System.out.print("Coche: " + brand[i + 1]
                            + " Ruido promedio " + results[2][i] + "\n");
                }
                System.out.print("\nPresione enter para continuar");
                in.readLine();
                break;
            case 3:
                for (int i = 0; i < noise.length; i++) {
                    System.out.print("velocidad " + noise[0][i]
                            + "km/h  Ruido promedio: " + results[3][i] + "\n");
                }
                System.out.print("\nPresione enter para continuar");
                in.readLine();
                break;
            case 4:

                System.out.print("El coche que produjo mas ruido fue "
                        + brand[(results[4][1] + 1)] + " con un promedio de "
                        + results[4][0] + "\n");
                System.out.print("El coche que produjo menos ruido fue "
                        + brand[(results[4][3] + 1)] + " con un promedio de "
                        + results[4][2] + "\n");
                System.out.print("La velocidad que produjo mas ruido fue "
                        + noise[0][results[4][5] + 1] + " km/h "
                        + "con un promedio de " + results[4][4] + "\n");
                System.out.print("La velocidad que produjo menos ruido fue "
                        + noise[0][results[4][7] + 1] + " km/h "
                        + "con un promedio de " + results[4][6] + "\n");

                System.out.print("\nPresione enter para continuar");
                in.readLine();
                break;
            case 5:
                quit = true;
                break;
            default:
                System.out.print("\nSeleccion erronea"
                        + "\nPresione enter para intentar de nuevo.");
                in.readLine();
                break;
            }
        }
    }

    public static int[][] operations(int noise[][]) {
        int results[][] = new int[5][8];
        // will store in the array 
        // 0 add all speeds in
        // 1 add all noise
        // 2 average speed
        // 3 average noise 
        // 4.0 biggest noise average
        // 4.1 biggestNoise average index
        // 4.2 smallestNoise average 
        // 4.3 smallestNoise average index
        // 4.4 speed average noise (biggest number)
        // 4.5 speed average noise (biggest number)index
        // 4.6 speed average noise (smallest number)
        // 4.7 speed average noise (smallest number)index
        //  index to keep track of the position of the biggest/smallest number


        for (int i = 0; i < noise.length; i++) {
            for (int j = 0; j < noise[0].length; j++) {
                results[0][i] += noise[i][j];
            }
            for (int j = 0; j < noise[0].length - 1; j++) {
                results[1][i] += noise[j + 1][i];
            }
        }

        for (int i = 0; i < noise.length - 1; i++) {
            // -1 por que el 0 en el array no es un coche (velocidad)
            results[2][i] = results[0][i + 1] / noise[0].length;
            // ruido promedio coche
        }

        for (int i = 0; i < noise.length; i++) {
            // ruido promedio velocidad
            results[3][i] = results[1][i] / noise[0].length;
        }
        results[4][0] = results[2][0];
        results[4][2] = results[2][0];
        results[4][4] = results[2][0];
        results[4][6] = results[2][0];
        results[4][7] = -1;// ok
        results[4][5] = -1; // wrong

        for (int i = 0; i < noise.length - 1; i++) {
            // -1 'cause in 0 is stored the word speed and not a car
            if (results[4][0] < results[2][i]) {
                results[4][0] = results[2][i];
                results[4][1] = i;
            }

        }
        for (int i = 0; i < noise.length - 1; i++) {
            if (results[4][2] > results[2][i]) {
                results[4][2] = results[2][i];
                results[4][3] = i;
            }
        }
        for (int i = 0; i < noise.length - 1; i++) {

            if (results[4][6] > results[3][i]) {
                results[4][6] = results[3][i];
                results[4][7] = i;  // this one works ok
            }
        }
        for (int i = 0; i < noise.length - 1; i++) {
            if (results[4][4] < results[3][i]) { 
                results[4][4] = results[3][i];
                results[4][5] = i;  // problem here nothing gets stored
            }
        }
        return results;
    }

}

Recommended Answers

All 2 Replies

results[4][5] = i; // problem here nothing gets stored

How do you know? Is the statement executed? It's inside of an if statement.
Add a println immediately after that statement that prints out the value of results[4][5] and the value of i. Then you'll see what was stored in the array.

thanks, you were right some of the if statements are not executing

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.