Error: java.lang.arrayindexoutofboundsexception: 15

-2

In my program marks this error and I do not know where the problem is.

What I try to do is generate 45 numbers of an integer and a decimal, save them in 3 arrays of 15 elements each and generate another arrangement of only 3 elements where the sum of each of the arrangements is saved and the final show the 4 arrangements in 4 consecutive columns.

This is my program:

    StdIn in = new StdIn ();  
    float[] PC = new float [15];  
    float[] SC = new float [30];
    float[] TC = new float [46];    
    int A, B, C;  
    float S;  
    for (A = 1 ; A < 15 ; A++)
    {
        System.out.println ("Primer Columna");
        System.out.print (A + "\tNumero: ");
        PC [A] = in.readFloat ();
    }
    for (B = 15 ; B < 30 ; B++)
    {
        System.out.println ("Segunda Columna");
        System.out.print (B + "\tNumero:\t");
        SC [B] = in.readFloat ();
    }
    for (C = 30 ; C < 46 ; C++)
    {
        System.out.println ("Tercer Columna");
        System.out.print (C + "\tNumero:\t");
        TC [C] = in.readFloat ();
    }
    System.out.println ("Primer Columna" + PC [A]);
    System.out.println ("Segunda Columna" + SC [B]);
    System.out.println ("Tercer COlumna" + TC [C]);
    S = PC [A] + SC [B] + TC [C];
    System.out.println ("Sumatoria de las 3 columnas" + S);
    } 
} 

And the line that marks "bad" after that error is this:

System.out.println ("Primer Columna" + PC [A]);
    
asked by Camila 26.07.2018 в 21:45
source

1 answer

0

here is another way to do it ... with two cycles one full and the other shows

StdIn in = new StdIn ();  
    float[] PC = new float [15];  
    float[] SC = new float [30];
    float[] TC = new float [46];    
    int A, B, C;  
    float S;  

    for (j=1; j <= 45 ; j++) {

        if (j <=15 ) {
            System.out.println ("Primer Columna");
            System.out.print (j + "\tNumero: ");
            PC [j] = in.readFloat ();
        }
        else if (j > 15 && j <= 30) {
            System.out.println ("Segunda Columna");
            System.out.print (j + "\tNumero:\t");
            SC [j] = in.readFloat ();
        }
        else if (j > 30 && j <= 45) {
            System.out.println ("Tercer Columna");
            System.out.print (j + "\tNumero:\t");
            TC [j] = in.readFloat ();
        }
    }
//aqui se muestran y se suman todas las filas de los arreglos


for (j = 1; j < PC.length ; j++) {
        System.out.println ("Primer Columna" + PC [j]);
        System.out.println ("Segunda Columna" + SC [j]);
        System.out.println ("Tercer COlumna" + TC [j]);
        S = PC [j] + SC [j] + TC [j];
        System.out.println ("Sumatoria de las 3 columnas" + S);
    }

} 

I hope you serve and mark it as it turns out for others to help with this ... NiceCode By: JJ

    
answered by 26.07.2018 в 22:52