Assigning Rows of a Document to a String Vector

0

I'm trying to try to assign to a string vector certain lines of document, the content of the document is this:

  

3
  50
  5,80,0
  15,5,1
  12,30,0

I want, from the third row, each row to be stored in a certain index of a String vector, for example:

Line 3 with data 5.80,0 would be assigned to linhasproc[i] , where i is greater than 2.

How could this be possible?

Follow the application code:

NOTE: I know that it is not possible to execute the linhasproc[i]= linha; line. But she gets the idea of what I want to do.

package trabalhoso;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.Scanner;
import java.util.Arrays;
import java.util.Random;

public class Lerprocessos {

    public static void main(String[] args) {
        Scanner ler = new Scanner(System.in);

        System.out.printf("Informe o nome de arquivo texto:\n");

        //Lê o nome do arquivo que será aberto
        String nome = ler.nextLine();
        System.out.printf("\nConteúdo do arquivo texto:\n");

        try {

            FileReader arq = new FileReader(nome);
            BufferedReader lerArq = new BufferedReader(arq);

            String linha = lerArq.readLine(); // lê a primeira linha
            // a variável "linha" recebe o valor "null" quando o processo
            // de repetição atingir o final do arquivo texto

            //Atribuindo o numero de processos, onde este se encontra na linha 0 do documento
            int numprocesso = Integer.parseInt(linha);

            int numciclo=0; //Variável que guardará o numero total de ciclos 

            int i=0;

            String linhasproc[]; //Vetor de String criado para guardar cada linha 
            //com as diferentes caracteristicas dos processos

            while (linha != null) {

                //Atribuindo o numero de ciclos, onde este se encontra na linha 1 do documento
                if(i==1){numciclo=Integer.parseInt(linha);}

                //lê da segunda até a última linha
                if(i>=2){
                    linhasproc[i]= linha;
                }

                linha=lerArq.readLine();
                i++;
            }

            System.out.println(numprocesso);
            System.out.println(numciclo);

            arq.close();

        } catch (IOException e) {
            System.err.printf("Erro na abertura do arquivo: %s.\n",
              e.getMessage());
        }

        System.out.println();
  }

}
    
asked by anonymous 06.06.2017 / 16:43

1 answer

0

Follow the solution the way you wanted to implement it. I've separated a method into a way to know what the size of your array will be, however you'll have to do some validation to check if a given row contains numbers, if the number of rows is greater than 2, and so on, being that on several occasions exceptions can occur, the ideal would be to treat them.

public static void main(String[] args) {
    Scanner ler = new Scanner(System.in);

    System.out.printf("Informe o nome de arquivo texto:\n");

    //Lê o nome do arquivo que será aberto
    String nome = ler.nextLine();
    System.out.printf("\nConteúdo do arquivo texto:\n");

    try {

        FileReader arq = new FileReader(nome);
        BufferedReader lerArq = new BufferedReader(arq);

        String linha = lerArq.readLine(); // lê a primeira linha
        // a variável "linha" recebe o valor "null" quando o processo
        // de repetição atingir o final do arquivo texto

        //Pega a quantidade de linhas para ser setado no array linhasproc
        int qtdlinhas = 0; 
        qtdlinhas = verificaQtdLinhas(nome);

        //Atribuindo o numero de processos, onde este se encontra na linha 0 do documento
        int numprocesso = Integer.parseInt(linha);

        int numciclo=0; //Variável que guardará o numero total de ciclos 

        int i=0;

        String linhasproc[] = new String[qtdlinhas-2]; //Vetor de String criado para guardar cada linha 
        //com as diferentes caracteristicas dos processos

        while (linha != null) {

            //Atribuindo o numero de ciclos, onde este se encontra na linha 1 do documento
            if(i==1){numciclo=Integer.parseInt(linha);}

            //lê da segunda até a última linha
            if(i>=2){
                linhasproc[i-2]= linha;
            }

            linha=lerArq.readLine();
            i++;
        }

        System.out.println(numprocesso);
        System.out.println(numciclo);

        arq.close();

    } catch (IOException e) {
        System.err.printf("Erro na abertura do arquivo: %s.\n",
          e.getMessage());
    }

    System.out.println();

}

private static int verificaQtdLinhas(String nome) throws IOException {
    FileReader arq = new FileReader(nome);
    BufferedReader lerArq = new BufferedReader(arq);
    String linha = lerArq.readLine();
    int qtdLinhas = 0;
    while(linha != null){
        qtdLinhas++;
        linha = lerArq.readLine();
    }
    arq.close();
    return qtdLinhas;
}
    
06.06.2017 / 23:13