Scanner - Split regardless of number of columns

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 2nd and 3rd line has 5 variables. With the code I did I can only do as it is in the image. That is to say when it has 5 words delimited by a space prints as if it were the designation of the aircraft of the previous.

    
asked by anonymous 03.11.2016 / 13:25

2 answers

1

Check with nextLine as in the example below, so you have control of the line and with split you 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();
    }
}
    
03.11.2016 / 13:35
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();
    }
}
    
03.11.2016 / 14:36