public static void loadfile(stock []a) throws IOException{
String fich;
File fichDad;
int i=countDatas(a);
while(sc.nextLine().length()!=0);
do{
System.out.print("\nQual é o nome do ficheiro que deseja ler?\n");
fich=sc.nextLine();
fichDad = new File(fich);
}while(!fichDad.canRead()||!fichDad.exists()||!fichDad.isFile());
Scanner lerFil = new Scanner (fichDad);
while (lerFil.hasNextLine()){
if(i==100) break;
if(!lerFil.hasNext()) break;;
a[i]=new stock();
a[i].nome=lerFil.next();
a[i].quant=lerFil.nextInt();
i++;
}
System.out.print("\n\n\nValores inseridos com sucesso!\n\n\n\n");
lerFil.close();
}
I'm doing an exercise that consists of a program (in Java) for stock maintenance in a store. The program is working, but there is a "problem" that I can not solve. The program does all the operations correctly and then the progress made can be saved in a file, which can then be read and loaded into the program to resume progress. However, if you read the file twice in a row, instead of deleting what you have in memory and typing it over, the function displays twice the contents of the file. Does anyone have a solution?