il problema è il seguente legato ad un esercizio di prova che stavo facendo per imparare a programmare.
ho la seguente classe studente che non fa altro che creare degli oggetti studenti composti da un campo nome ed uno voto prendendo queste informazioni da un file.
codice:
public class Studente {
public Studente(String nome, int media)throws Exception {
this.nome = nome;
this.media = media;
}
public static Studente leggi(BufferedReader br)throws Exception {
String dato;
int voto;
dato = br.readLine();
voto = Integer.parseInt(br.readLine());
System.out.println(dato);
System.out.println(voto);
return new Studente(dato,voto);
}
private String nome;
private int media;
}
ora se mi volgio stampare tutti gli studenti che ho preso dal file se utilizzo il seguente codice con il for funziona perchè conosco il numero di elementi da stampare ma se nn conosco il numero di elementi che era presente nel file utilizzando while come faccio?? se utilizzo while nel codice seguente mi da il seguente errore:
java.lang.NumberFormatException: null
at java.lang.Integer.parseInt(Integer.java:382)
at java.lang.Integer.parseInt(Integer.java:463)
at esercizi3.Studente.leggi(Studente.java:15)
at esercizi3.Caricare.main(Caricare.java:20)
stampa tutto ciò che è nel file
Exception in thread "main"
codice:
public static void main(String[] args)throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream("registro.txt")));
Studente stu = Studente.leggi(br);
while (stu != null)
stu = Studente.leggi(br);