简体   繁体   中英

Vector out of bounds while reading a TXT file

I'm trying to read the values of a .TXT file and store these values in various vectors (the values in the file are separated by a comma, I'm using .Split () method to get these values), the problem that is appearing to me is that my vector aux is exceeding the limit after the first writing.

The code:

String[] iltxt = new String[49];
String[] ertxt = new String[49];
String[] tcsetxt = new String[49];
String[] arttxt = new String[49];
String[] rmtxt  = new String[49];
String[] burtxt  = new String[49];
String[] toftxt = new String[49];
String[] atgtxt = new String[49];
String[] nrtxt = new String[49];
String[] hlab27txt = new String[49];
String[] djtxt = new String[49];
String[] dltxt = new String[49];
String[] rctxt = new String[49];
String[] dctxt = new String[49];
String[] mobtxt = new String[49];
String[] dtstxt = new String[49];
String[] sintxt = new String[49];
String[] diagnosticotxt = new String[49];
String[] aux = new String[18];
int cont = 1;  
      try {
      FileReader arq = new FileReader("File.txt");
      BufferedReader lerArq = new BufferedReader(arq);
      String linha = lerArq.readLine(); 
      aux = linha.split(",");
      //STORES THE VALUES OF THE FIRST LINE INTO THE VECTORS
      dltxt[0] = aux[0];
      rctxt[0] = aux[1];
      dctxt[0] = aux[2];
      mobtxt[0] = aux[3];
      dtstxt[0] = aux[4];
      iltxt[0] = aux[5];
      ertxt[0] = aux[6];
      tcsetxt[0] = aux[7];
      arttxt[0] = aux[8];
      rmtxt[0] = aux[9];
      burtxt[0] = aux[10];
      toftxt[0] = aux[11];
      sintxt[0] = aux[12];
      atgtxt[0] = aux[13];
      nrtxt[0] = aux[14];
      hlab27txt[0] = aux[15];
      djtxt[0] = aux[16];
      diagnosticotxt[0] = aux[17];
      //CLEAN THE VECTOR AUX TO THE NEXT READ
      aux = null;
      while (linha != null) {
            //READ THE VALUES OF THE SECOND TO THE LAST LINE AND STORES INTO THE VECTORS
            linha = lerArq.readLine(); 
            aux = linha.split(",");
            dltxt[cont] = aux[0];
            rctxt[cont] = aux[1];
            dctxt[cont] = aux[2];
            mobtxt[cont] = aux[3];
            dtstxt[cont] = aux[4];
            iltxt[cont] = aux[5];
            ertxt[cont] = aux[6];
            tcsetxt[cont] = aux[7];
            arttxt[cont] = aux[8];
            rmtxt[cont] = aux[9];
            burtxt[cont] = aux[10];
            toftxt[cont] = aux[11];
            sintxt[cont] = aux[12];
            atgtxt[cont] = aux[13];
            nrtxt[cont] = aux[14];
            hlab27txt[cont] = aux[15];
            djtxt[cont] = aux[16];
            diagnosticotxt[cont] = aux[17];
            //CLEAN THE AUX VECTOR
            aux = null;
            //INCREMENT THE COUNTER TO SAVE VALUES IN THE NEXT POSITIONS OF THE VECTORS
            cont++;
      }
      arq.close();
      } catch (IOException e) {
            System.err.printf("Error while open the file: %s.\n",
            e.getMessage());
      }

The values of the TXT File:

Sim,Sim,Sim,Ilimitado,Sim,Ausente,Moderado,Moderado,Não,Não,Não,Não,Não,Não,Não,Negativo,Não,Espondilite Anquilosante

Não,Não,Não,Normal,Não,Ausente,Moderado,Não,Sim,Não,Não,Não,Não,Sim,Não,Não,Não,Artrite de Gota

Não,Não,Não,Limitado,Não,Ausente,Moderado,Não,Não,Não,Não,Sim,Não,Sim,Não,Não,Não,Artrite de Gota

Não,Não,Não,Normal,Não,Moderado,Importante,Moderado,Não,Não,Não,Não,Não,Sim,Não,Não,Não,Artrite Reumatica

Não,Não,Não,Limitado,Não,Muito Importante,Muito Importante,Importante,Sim,Não,Não,Sim,Sim,Sim,Não,Não,Não,Artrite de Gota

Não,Não,Não,Limitado,Não,Moderado,Muito Importante,Não,Sim,Não,Não,Não,Sim,Sim,Não,Não,Não,Artrite Reumática

Sim,Não,Sim,Normal,Sim,Moderado,Importante,Importante,Não,Não,Não,Não,Não,Não,Não,Positivo,Não,Espondilite Anquilosante 

Sim,Sim,Sim,Limitado,Sim,Moderado,Importante,Não,Não,Não,Não,Não,Não,Não,Não,Positivo,Não,Espondilite Anquilosante

Não,Não,Não,Limitado,Não,Muito Importante,Ausente,Não,Sim,Não,Não,Não,Sim,Sim,Não,Não,Não,Artrite de Gota

Não,Não,Não,Limitado,Não,Importante,Ausente,Leve,Sim,Sim,Não,Não,Sim,Sim,Não,Negativo,Não,Artrite Reumatica

Sim,Não,Não,Normal,Não,Leve,Muito Importante,Não,Não,Não,Não,Não,Não,Não,Não,Negativo,Não,Espondilite Anquilosante

Não,Não,Não,Normal,Não,Leve,Importante,Não,Sim,Sim,Não,Não,Sim,Sim,Não,Não,Não,Artrite Reumática

Sim,Não,Não,Normal,Não,Moderado,Ausente,Não,Sim,Não,Não,Não,Sim,Sim,Não,Positivo,Não,Espondilite Anquilosante

Não,Não,Não,Limitado,Não,Importante,Ausente,Moderado,Sim,Sim,Não,Não,Sim,Sim,Não,Não,Sim,Artrite Reumatica

Sim,Sim,Não,Normal,Sim,Ausente,Moderado,Leve,Não,Sim,Não,Não,Não,Não,Não,Positivo,Não,Espondilite Anquilosante

Sim,Não,Sim,Normal,Não,Ausente,Importante,Não,Não,Não,Não,não,Não,Não,Não,Positivo,Não,Espondilite Anquilosante

Não,Não,Não,Normal,Não,Importante,Ausente,Não,Não,Não,Sim,Não,Não,Sim,Sim,Não,Não,Artrite Reumatica

Sim,Sim,Não,Normal,Não,Leve,Importante,Importante,Não,Não,Não,Não,Não,Não,Não,Positivo,Não,Espondilite Anquilosante

Não,Não,Não,Limitado,Não,Moderado,Moderado,Moderado,Sim,Não,Não,Não,Não,Sim,Não,Não,Não,Artrite de Gota

Não,Não,Não,Limitado,Não,Importante,Importante,Moderado,Sim,Não,Sim,Sim,Sim,Sim,Não,Não,Não,Artrite de Gota

Não,Não,Não,Normal,Não,Importante,Importante,Não,Sim,Sim,Sim,Sim,Não,Sim,Não,Não,Não,Artrite de Gota

Sim,Sim,Não,Normal,Sim,Ausente,Importante,Importante,Não,Sim,Não,Não,Não,Sim,Não,Positivo,Não,Espondilite Anquilosante

Não,Não,Não,Normal,Não,Importante,Moderado,Não,Sim,Não,Sim,Sim,Sim,Sim,Não,Não,Não,Artrite de Gota

Não,Não,Não,Limitado,Não,Moderado,Ausente,Não,Não,Não,Sim,Sim,Não,Não,Não,Não,Não,Artrite de Gota

Não,Não,Não,Normal,Não,Importante,Importante,Não,Sim,Não,Não,Não,Sim,Sim,Não,Negativo,Não,Artrite Reumatica

Não,Não,Não,Limitado,Não,Importante,Moderado,Moderado,Sim,Não,Não,Não,Sim,Sim,Não,Não,Não,Artrite Reumatica

Sim,Não,Não,Normal,Não,Moderado,Importante,Não,Não,Não,Não,Não,Não,Não,Não,Positivo,Não,Espondilite Anquilosante

Não,Não,Não,Limitado,Não,Ausente,Ausente,Não,Sim,Não,Não,Não,Sim,Sim,Não,Negativo,Não,Artrite Reumática

Sim,Sim,Não,Limitado,Sim,Leve,Importante,Não,Não,Sim,Não,Não,Não,Não,Não,Negativo,Não,Espondilite Anquilosante

Não,Não,Não,Limitado,Não,Importante,Moderado,Não,Sim,Sim,Não,Não,Sim,Sim,Não,Não,Não,Artrite Reumatica

Sim,Sim,Não,Limitado,Não,Importante,Moderado,Não,Não,Não,Não,Não,Não,Não,Não,Positivo,Não,Espondilite Anquilosante

Sim,Não,Não,Normal,Não,Ausente,Moderado,Moderado,Não,Não,Não,Não,Não,Sim,Não,Não,Não,Artrite de Gota

Não,Não,Não,Limitado,Não,Moderado,Moderado,Não,Sim,Sim,Sim,Sim,Sim,Sim,Não,Não,Não,Artrite de Gota

Sim,Sim,Não,Limitado,Sim,Importante,Muito Importante,Importante,Sim,Sim,Não,Não,Sim,Sim,Não,Não,Não,Artrite Reumatica

Não,Não,Não,Limitado,Não,Muito Importante,Ausente,Moderado,Sim,Sim,Não,Não,Sim,Sim,Sim,Não,Não,Artrite Reumática

Não,Não,Não,Limitado,Não,Leve,Moderado,Não,Sim,Não,Não,Não,Sim,Sim,Não,Não,Não,Artrite Reumatica

Não,Não,Não,Limitado,Não,Moderado,Não,Sim,Não,Não,Sim,Não,Sim,Não,Não,Não,Artrite de Gota

Sim,Sim,Sim,Limitado,Não,Moderado,Muito Importante,Moderado,Sim,Não,Não,Não,Não,Sim,Não,Positivo,Sim,Espondilite Anquilosante

Não,Não,Não,Normal,Não,Leve,Importante,Não,Sim,Não,Não,Sim,Sim,Sim,Não,Não,Não,Artrite de Gota

Sim,Não,Não,Normal,Não,Ausente,Moderado,Moderado,Não,Não,Não,Não,Não,Sim,Não,Positivo,Não,Espondilite Anquilosante

Sim,Sim,Não,Normal,Não,Ausente,Ausente,Não,Não,Sim,não,Não,Não,Sim,não,Não,Não,Artrite Reumatica

Sim,Sim,Sim,Normal,Não,Ausente,Importante,Não,Não,Não,Não,Não,Não,Não,Não,Positivo,Não,Espondilite Anquilosante

Sim,Não,Não,Normal,Sim,Moderado,Importante,Não,Não,Sim,Não,Não,Não,Não,Não,Positivo,Não,Espondilite Anquilosante

Não,Não,Não,Limitado,Não,Leve,Importante,Não,Sim,Sim,Não,Não,Sim,Sim,Não,Não,Não,Artrite Reumatica

Não,Não,Não,Limitado,Não,Leve,Moderado,Não,Sim,Sim,Não,Não,Sim,Sim,Não,Não,Não,Artrite Reumatica

Não,Não,Não,Limitado,Não,Moderado,Importante,Não,Sim,Não,Não,Não,Sim,Sim,Não,Não,Não,Artrite Reumatica

Não,Não,Não,Normal,Não,Importante,Importante,Não,Não,Sim,Não,Não,Não,Sim,Não,Não,Não,Artrite Reumatica

Sim,Não,Não,Normal,Não,Leve,Importante,Não,Sim,Não,Não,Não,Sim,Sim,Não,Não,Não,Artrite de Gota

Não,Não,Não,Normal,não,Moderado,Importante,Não,Não,Sim,Não,não,Não,Sim,Não,Não,Não,Artrite Reumatica

The error that appears while executing the code:

Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 1

The line of the error:

rctxt[cont] = aux[1];

Someone knows how to fix this? (Sorry about some words in portugues, I am from Brazil)

Looks like some of the lines do not have any text (empty lines).

The split on these lines will not produce an 18 length Strings array.

try

if (arr.length < 18)
   continue;

Also as mentioned above, having an Object that encapsulates this data would make you code a lot more readable.

Edit

For clarification

You have this code

 while (linha != null) {
        linha = lerArq.readLine(); 
        aux = linha.split(",");

now check the size of the aux array and if not 18 then skip this line

    if (aux.length < 18) {
       System.err.println (linha + " does not have enough values");
       continue;
    }

The exception happens because you seperated the lines with empty lines and you do not check if there are actually 18 tokens. Either you remove those empty lines or you add a condition like so:

linha = lerArq.readLine();

aux = linha.split(",");
if(aux.length != 18)
    continue;

Also you have a little problem with one of your lines (35). That line has just 17 tokens and causes the next ArrayOutOfBoundsException if you do not use the condition but remove the empty lines in your File.txt

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM