Doubts with java.lang.ArrayIndexOutOfBoundsException

2

I'm trying to make a program in which of 7 files containing 1,048,576 rows and columns make several files:

Orbita42-7columnsNorm22: 1.048576 rows and 7 columns (works well).

Composition2: summarizes how many times the Euc, Dio and How cases come out (it turns out well) what happens is that it only analyzes 273 cases and I do not know why.

Differences: 1,048,576 rows in which I say row by row if we are in a case Euc, Dio or How and in this file I also have problems.

At the time of executing the code, I get the following error:

  

"1.2425816 Exception in thread" main "1.0 0.90949553 0.7077151   0.5749258 0.35534126 0.30860534 java.lang.ArrayIndexOutOfBoundsException: 1 at   com.berni.filtromodif.FiltroUnicoModifNorm2.main (UnicodeModifNorm2.java:113)

"In case you refer to line 113 the error that line is as follows:

dif_euc[i][1]= (float) Math.sqrt(Math.pow((normaliz[i][0]-0.88126099),2)
+Math.pow((normaliz[i][1]-1),2)
+Math.pow((normaliz[i][2]-1.090194054),2)
+Math.pow((normaliz[i][3]-1.186721261),2)
+Math.pow((normaliz[i][4]-1.080425652),2)
+Math.pow((normaliz[i][5]-0.771797639),2)
+Math.pow((normaliz[i][6]-0.839310604),2));

This case always has to give 0 in case it is the origin of the problem: Math.pow ((normaliz [i] [1] -1), 2)

Could someone please tell me: 1) What is the error? 2) How could I generate the Composition2 and Differences files by traversing the 1,048,576 rows of the file Orbita42-7columnsNorm22?

A thousand thanks in advance!,

I attached the code, (a thousand thanks again):

    package com.berni.filtromodif;

    import java.io.BufferedReader;
    import java.io.File;
    import java.io.FileNotFoundException;
    import java.io.FileReader;
    import java.io.FileWriter;
    import java.io.IOException;
    import java.io.PrintWriter;
    import java.util.Scanner;

    public class FiltroUnicoModifNorm2 {

    public static void main(String[] args) throws IOException {

        int n=1048576;       
        //float datos[][] = new float[n][8];
        float datos[][] = new float[n][7];

        //Definimos nuevas variables:
        int Euc=0;
        int Dio=0;
        int How=0;

        int Euc_total=0;
        int Dio_total=0;
        int How_total=0;

        float dif_euc[][]= new float[n][1];
        float dif_dio[][]= new float[n][1];
        float dif_how[][]= new float[n][1];

        float normaliz[][] = new float[n][7];

        //Scanner sc1,sc2,sc3,sc4,sc5,sc6,sc7,sc8;
        Scanner sc2,sc3,sc4,sc5,sc6,sc7,sc8;

        try {
            //sc1 = new Scanner(new File("C:\Users\jose\Desktop\ÓrbitasRepetición\HAMO, Ciclo 6, Órbita 2\berni\f1.txt"));
            sc2 = new Scanner(new File("C:\Users\jose\Desktop\Órbitas Vesta\HAMO, Ciclo 6, Órbita 4-2\berni\f2.txt"));
            sc3 = new Scanner(new File("C:\Users\jose\Desktop\Órbitas Vesta\HAMO, Ciclo 6, Órbita 4-2\berni\f3.txt"));
            sc4 = new Scanner(new File("C:\Users\jose\Desktop\Órbitas Vesta\HAMO, Ciclo 6, Órbita 4-2\berni\f4.txt"));
            sc5 = new Scanner(new File("C:\Users\jose\Desktop\Órbitas Vesta\HAMO, Ciclo 6, Órbita 4-2\berni\f5.txt"));
            sc6 = new Scanner(new File("C:\Users\jose\Desktop\Órbitas Vesta\HAMO, Ciclo 6, Órbita 4-2\berni\f6.txt"));
            sc7 = new Scanner(new File("C:\Users\jose\Desktop\Órbitas Vesta\HAMO, Ciclo 6, Órbita 4-2\berni\f7.txt"));
            sc8 = new Scanner(new File("C:\Users\jose\Desktop\Órbitas Vesta\HAMO, Ciclo 6, Órbita 4-2\berni\f8.txt"));

            FileWriter fw;
            fw = new FileWriter(new File("C:\Users\jose\Desktop\Órbitas Vesta\HAMO, Ciclo 6, Órbita 4-2\TXT Modifica2\Orbita42-7columnasNorm22.txt"));

            //aquí escribiríamos otro fichero indicando el resultado si es eucrite/diogenite/howardite
            FileWriter fx;
            fx = new FileWriter(new File("C:\Users\jose\Desktop\Órbitas Vesta\HAMO, Ciclo 6, Órbita 4-2\TXT Modifica2\Composicion2.txt"));

            FileWriter fy;
            fy = new FileWriter(new File("C:\Users\jose\Desktop\Órbitas Vesta\HAMO, Ciclo 6, Órbita 4-2\TXT Modifica2\Diferencias.txt"));


            for(int i=0;i<n;i++){

                //if (sc1.hasNextFloat()) {
                //    datos[i][0] = sc1.nextFloat();
                //}


                if (sc8.hasNextFloat()) {
                    datos[i][0] = sc8.nextFloat();
                }


                if (sc2.hasNextFloat()) {
                    datos[i][1] = sc2.nextFloat();
                }


                if (sc7.hasNextFloat()) {
                    datos[i][2] = sc7.nextFloat();                 
                }


                if (sc3.hasNextFloat()) {
                    datos[i][3] = sc3.nextFloat();                 
                }


                if (sc6.hasNextFloat()) {
                    datos[i][4] = sc6.nextFloat();                 
                }


                if (sc4.hasNextFloat()) {
                    datos[i][5] = sc4.nextFloat();                 
                }


                if (sc5.hasNextFloat()) {
                    datos[i][6] = sc5.nextFloat();                  
                }


                //for(int j=0;j<8;j++){
                for(int j=0;j<7;j++){
                    normaliz[i][j]=(datos[i][j]/datos[i][1]);
                    System.out.print(normaliz[i][j] + " ");
                    fw.write(normaliz[i][j] + " ");

                }

                System.out.println();
                fw.write(System.lineSeparator());


               dif_euc[i][1]= (float) Math.sqrt(Math.pow((normaliz[i][0]-0.88126099),2)+Math.pow((normaliz[i][1]-1),2)+Math.pow((normaliz[i][2]-1.090194054),2)+Math.pow((normaliz[i][3]-1.186721261),2)
                        +Math.pow((normaliz[i][4]-1.080425652),2)+Math.pow((normaliz[i][5]-0.771797639),2)+Math.pow((normaliz[i][6]-0.839310604),2)); 

               dif_dio[i][1]= (float) Math.sqrt(Math.pow((normaliz[i][0]-0.721571227),2)+Math.pow((normaliz[i][1]-1),2)+Math.pow((normaliz[i][2]-1.086566669),2)+Math.pow((normaliz[i][3]-1.04693793),2)
                        +Math.pow((normaliz[i][4]-0.734598492),2)+Math.pow((normaliz[i][5]-0.425308693),2)+Math.pow((normaliz[i][6]-0.537498964),2));

               dif_how[i][1]= (int) Math.sqrt(Math.pow((normaliz[i][0]-0.843658437),2)+Math.pow((normaliz[i][1]-1),2)+Math.pow((normaliz[i][2]-1.088089101),2)+Math.pow((normaliz[i][3]-1.09256438),2)
                        +Math.pow((normaliz[i][4]-0.737644446),2)+Math.pow((normaliz[i][5]-0.405681366),2)+Math.pow((normaliz[i][6]-0.487664752),2));

               if (dif_euc[i][1]<dif_dio[i][1]){
                   if (dif_euc[i][1]<dif_how[i][1]){
                        Euc=Euc+1;

                        //fy.write("Euc");
                        //fy.write(System.lineSeparator());
                        }
               }

               else{
                    if(dif_dio[i][1]<dif_euc[i][1]){
                        if(dif_dio[i][1]<dif_how[i][1]){
                            Dio=Dio+1;

                            //fy.write("Dio");
                            //fy.write(System.lineSeparator());

                        }
                            }

                    else{                               
                         if(dif_how[i][1]<dif_euc[i][1]){
                             if(dif_how[i][1]<dif_dio[i][1]){
                                    How=How+1;


                                }
                         }
                            }
                        }

                }

            fw.flush();
            fw.close();

            Euc_total=(Euc*100)/(Euc+Dio+How);
            Dio_total=(Dio*100)/(Euc+Dio+How);
            How_total=(How*100)/(Euc+Dio+How);

            System.out.println(Euc);
            System.out.println(Dio);
            System.out.println(How);

            System.out.println(Euc_total);
            System.out.println(Dio_total);
            System.out.println(How_total);


            fx.write("["+"(Euc_total "+Euc_total+ ") " + "(Euc "+Euc +")"+ "]" + "\t "
                      + "["+"(Dio_total "+Dio_total+ ") " + "(Dio "+Dio +")"+ "]" + "\t "
                      + "["+"(How_total "+How_total+ ") " + "(How "+How +")"+ "]" );
            fx.flush();
            fx.close();

            fy.flush();
            fy.close();


        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        finally{
        }       



    }


} 
    
asked by Ber Tsacianegu del Tepuy 09.08.2016 в 17:48
source

1 answer

0

In this line you declare dif_euc as an array of size n in the first dimension and 1 in the second dimension:

    float dif_euc[][]= new float[n][1];

In this line you access item 1 in the second index:

    dif_euc[i][1]= (float) Math.sqrt(Math.pow((normaliz[i][0]-0.88126099),2)+Math.pow((normaliz[i][1]-1),2)+Math.pow((normaliz[i][2]-1.090194054),2)+Math.pow((normaliz[i][3]-1.186721261),2)
                    +Math.pow((normaliz[i][4]-1.080425652),2)+Math.pow((normaliz[i][5]-0.771797639),2)+Math.pow((normaliz[i][6]-0.839310604),2)); 

Which will always give an out-of-bounds exception, because the second index must be smaller than the size of the second dimension; which is 1.

I would not give you an error if you use dif_euc[i][0] or if you initialize the array as new float[n][2] .

The same thing happens with dif_dio and dif_how .

    
answered by 09.08.2016 / 18:23
source