breaking a file and storing it in a vector - in Java

Asked

Viewed 2,006 times

0

I am trying to store in an array the values of a file with the extension csv. This extension comes from a file made in excel, and when saved in this extension the values are separated through ";".

inserir a descrição da imagem aqui

Through this image you can already imagine the final result you would like to arrive with the matrix.

Then, after reading the file, it follows my logic:

1 - Break row by row of this file I read.

2 - Store each row in a single vector using the split method, placing as parameter the delimiter (";").

3 - Pass from vector to matrix.

The problem is that when I store line by line in the vector, it stores only the last line I am inserting.

inserir a descrição da imagem aqui (Netbeans Print Console)

That is, instead of accumulating the entered values, it is overwriting the values each time the sequence goes to the bottom line.

Well, if I’m not getting past this part, it’s impossible to store the values inside the matrix. And please, I know that there are other solutions in Java to facilitate this process. But I’m really trying to work with simple logic and resources to get where I need to go.

So my difficulty is in storing each value in the vector using the split method().

Finally, follow the code I’ve already made!

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

public class EntradaSaidaIO {
    public static void main(String args[]) throws IOException{
        String matriz[][] = new String[26][26]; // Matriz onde contém 26 linhas e 26 colunas
        int n = 26*26; //
        String linhaF[] = new String[n]; //Vetor onde deveria armazenar os valores do arquivo

        InputStream e = new FileInputStream("C:\\Users\\Moraes\\Desktop\\MatrizApagar.csv");
        InputStreamReader er = new InputStreamReader(e);
        BufferedReader ebr = new BufferedReader(er);
        String texto = ebr.readLine(); // O método readLine()apens lê uma linha do arquivo

        while( texto != null){
            System.out.println(texto);
            linhaF = texto.split(";");
            /*
            for(int k = 0; k < linhaF.length; k++){
                System.out.print(linhaF[k]); // Apenas exibe o conteúdo dentro do vetor linhaF
            }
            */
            texto = ebr.readLine();
        }

        for(int i = 0; i < linhaF.length; i++){
            System.out.print(linhaF[i]); // Exibe todo o conteúdo armazenado
        }
        ebr.close();
    }
}
  • 3

    You’re right, there are other better ways to do this but as you’re putting as a requirement that you can’t use the better forms I won’t risk answering. I can do something that solves the problem and you say that so you don’t want and I lost the job. In addition, it would help to provide means of testing if you had put the data as text.

  • Could you explain the "he’s overriding the values every time the sequence goes to the bottom line"? Except for my distraction, it seems to me that nowhere in your code are the matrix lines being fed.

  • bigown... It is a requirement, but in case I see that really has no way to go through the logical process, I use resource in a good. And "have put the data as text", how so? Have saved the file as a . txt, and not as a . csv ? Thanks for the help!!!

  • Bacco... there’s no way I can feed an array if I can’t break my file and save it to a vector. For me to save in the matrix, first I would like to save in the vector this data. What you didn’t understand is that by saving the data in the vector, when going to the second line of the file the data that had been saved in the vector are overwritten by the new data from the next line of the file. Thus, the final result presents only the last line of the file. Improved now?

1 answer

2

The problem is on line 20:

linhaF = texto.split(";");

A 26*26 vector has been created but it is being replaced with each iteration by the return of texto.split(";"), which is a size 26 vector, when your intention was to add it to the end. The vector does not have a native form (that I know of) to concatenate another vector, so you will have to do this manually:

 int j = 0;
 while(texto != null){
      System.out.println(texto);
      for(String str : texto.split(";")){
            linhaF[j++] = str;                  
      }             
      ...                
 }

Another way would be to work with some kind of Java List instead of the String vector:

ArrayList<String> linhaF = new ArrayList<String<();
...
while( texto != null) {
    linhaF.addAll(Arrays.asList(texto.split(";")));
    //Arrays
    ...
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.