Scanner - Split regardless of the number of columns

Asked

Viewed 131 times

2

public void lerFicheiro(){

    try {
        Scanner scanner = new Scanner(ficheiro);

        while(scanner.hasNextLine()){
            //quando numero de campos for igual a 4

            String designacaoAeronave = scanner.next();
            String capacidadeDeposito = scanner.next();
            String conteudoAtualDeposito = scanner.next();
            String consumo = scanner.next();

            System.out.println(designacaoAeronave);
            System.out.println(capacidadeDeposito);
            System.out.println(conteudoAtualDeposito);
            System.out.println(consumo);
            System.out.println("----");
        }
        scanner.close();

    } catch (FileNotFoundException e) {

        e.printStackTrace();
    }
}

I have the following file:

UPT100 100 50 5

CPT100 100 11 5 20

PPT100 100 5 5 120

But the first line has 4 variables to save and the second and the third line has 5 variables. With the code I can only do as it is in the image. That is when it has 5 words delimited by a space prints as if it were the aircraft designation of the previous.

inserir a descrição da imagem aqui

  • I didn’t even understand your code, you’re manipulating file reading with Scanner?

  • Have you ever tried using nextLine() and split by space? I think that it is easier for you to assign the information using the line break as delimiter

  • yes that’s right.

  • @Sorack nextLine will read the whole row and what I want is whenever it has 5 "columns" save 5 variables, whenever it has 4 "columns" save 4 variables

  • Try the solution I posted below

2 answers

1


Check with the nextLine as in the example below, so you have control of the line and with the split can check how much information it contains and decide what to do with the information:

public void lerFicheiro(){

    try {
        Scanner scanner = new Scanner(ficheiro);

        while(scanner.hasNextLine()){
            String linha = scanner.nextLine();
            String[] informacoes = linha.split(" ");
            String novaInfo;

            String designacaoAeronave = informacoes[0];
            String capacidadeDeposito = informacoes[1];
            String conteudoAtualDeposito = informacoes[2];
            String consumo = informacoes[3];

            if (informacoes.length > 4) {
              novaInfo = informacoes[4];
            }

            System.out.println(designacaoAeronave);
            System.out.println(capacidadeDeposito);
            System.out.println(conteudoAtualDeposito);
            System.out.println(consumo);
            System.out.println("----");
        }
        scanner.close();

    } catch (FileNotFoundException e) {

        e.printStackTrace();
    }
}
  • this solution is correct but in this case the variables have two different names you understand. I managed to solve so

1

public void lerFicheiro(){

    try {
        Scanner scanner = new Scanner(ficheiro);

        while(scanner.hasNextLine()){
            //quando numero de campos for igual a 4

            String designacaoAeronave = scanner.next();
            int capacidadeDeposito = scanner.nextInt();
            int conteudoAtualDeposito = scanner.nextInt();
            int consumo = scanner.nextInt();

            System.out.println(designacaoAeronave);
            System.out.println(capacidadeDeposito);
            System.out.println(conteudoAtualDeposito);
            System.out.println(consumo);


            if(designacaoAeronave.startsWith("C")){                 //se for aviao de carga tem mais a tonelagem
                int tonelagem = scanner.nextInt();

                System.out.println(tonelagem);
            }

            if(designacaoAeronave.startsWith("P")){                 //se for aviao de passageiro tem mais a lotacao maxima
                int lotacaoMaxima = scanner.nextInt();

                System.out.println(lotacaoMaxima);
            }

        }
        scanner.close();

    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }
}

Browser other questions tagged

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