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.
I didn’t even understand your code, you’re manipulating file reading with Scanner?
– user28595
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
– Sorack
yes that’s right.
– rrr
@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
– rrr
Try the solution I posted below
– Sorack