0
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 am doing an exercise that consists of a program (in Java) for stock maintenance in a store. The program is working, but there’s a "problem" I can’t solve. The program does all 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 files twice in a row, instead of deleting what you have in your memory and writing over it, the function displays twice the contents of the file. Someone has a solution?
can share your reading code?
– Ricardo Pontual
I’m sorry I forgot to add. I already added
– Martim Neves
What is
countDatas(a)
?– Victor Stafusa
Where does the variable come from
sc
? 'Cause you clean her entire entrance?– Victor Stafusa
countDatas(a) is a function that counts the number of elements in the array. sc is the scanner
– Martim Neves