breaking a file and storing in a vector - in Java

0

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

Throughthisimageyoucanalreadyimaginethefinalresultthatyouwouldliketoarrivewiththematrix.

Following,afterreadingthefile,followsmylogic:

1-BreaklinebylineofthisfileIread.

2-Storeeachrowinasinglevectorusingthesplitmethod,settingthedelimiter(";") as a parameter.

3 - Move from array to array.

The problem is that when I store row by row in vector, it stores only the last row I'm inserting.

(PrintNetBeansconsole)

Thatis,insteadofaccumulatingthevaluesentered,itisoverlappingthevalueseachtimethesequencegoesdown.

Well,ifIcannotgetpastthispart,itisimpossibletostorethevaluesinsidethearray.And,please,IknowthereareothersolutionsinJavatofacilitatethisprocess.However,I'mreallytryingtoworkwithlogicandsimplefeaturestogetwhereIneedtogo.

So,mydifficultyistostoreeachvalueinthevectorusingthesplit()method.

Finally,followthecodeI'vealreadydone!

importjava.io.BufferedReader;importjava.io.FileInputStream;importjava.io.IOException;importjava.io.InputStream;importjava.io.InputStreamReader;publicclassEntradaSaidaIO{publicstaticvoidmain(Stringargs[])throwsIOException{Stringmatriz[][]=newString[26][26];//Matrizondecontém26linhase26colunasintn=26*26;//StringlinhaF[]=newString[n];//VetorondedeveriaarmazenarosvaloresdoarquivoInputStreame=newFileInputStream("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();
    }
}
    
asked by anonymous 07.03.2015 / 22:43

1 answer

2

The problem is on line 20:

linhaF = texto.split(";");

A vector of 26 * 26 has been created but it is being replaced at each iteration by the return of texto.split(";") , which is a vector of size 26, when its 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'll 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
    ...
}
    
08.03.2015 / 04:59