Ciao ragazzi, ho scritto questo piccolo programmino per un testo d'esame che sto cercando di risolvere
In pratica il programma legge da file, degli studenti formattati in un certo modo.
Inizialmente dichiaro tutti gli attributi come String (Sto usando readLine() di bufferedReader) e glieli faccio leggere.
Il programma stampa tutti gli attributi tranquillamente.
Ora però, ad un passo successivo, il testo mi chiede di calcolare la media dei voti (votoPI sta per programmazione internet, votoLPI sta per lab.progr.internet) e quindi devo fare la media dei due voti per ottenere il voto complessivo di questo esame.
Avevo letto che si può usare il metodo parseInt per fargli leggere le mie attuali strighe come interi ma non saprei dove mettere il codice.
A naso ho scritto questo codice che vorrei inserire
__________________________________________________ ____________________
//SECONDA PARTE, CALCOLO MEDIA
try
{
int pi = Integer.parseInt(votoPI);
int lpi=Integer.parseInt(votoLPI);
float media=(pi+lpi)/2;
System.out.println (media);
}
catch (NumberFormatException e)
{
System.out.println ("La stringa NON contiene un intero valido");
}
__________________________________________________ ______________________
Questo invece è il programmino:
codice:
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.StringTokenizer;
public class GestioneVotiPI {
public static void main(String[] args) throws IOException {
//FileNotFOund?
try{
FileReader fr=new FileReader("DatabaseVoti.txt");
BufferedReader br=new BufferedReader(fr);
ArrayList<Studente> listaStudenti=new ArrayList<Studente>();
System.out.println("**********");
String rigaLetta=br.readLine();
while(rigaLetta!=null){
StringTokenizer st=new StringTokenizer(rigaLetta, "-");
String nome=st.nextToken();
String cognome=st.nextToken();
String matricola=st.nextToken();
String votoPI=st.nextToken(); // Integer.parseInt
String votoLPI=st.nextToken(); // Integer.parseInt
try{
Studente temp=new Studente(nome, cognome, matricola, votoPI,votoLPI);
listaStudenti.add(temp);
}
catch( NumberFormatException e )
{
System.out.println( "Errore DA DECIFRARE NEL CODICE: " + rigaLetta + "; record" );
}
rigaLetta = br.readLine( );
}
br.close( );
for(Studente elementi:listaStudenti)
System.out.println(elementi.nome + " " + elementi.cognome+" "+elementi.matricola+" "+elementi.votoPI+" "+elementi.votoLPI);
System.out.println("");
System.out.println("**********");
}catch(IOException e){
System.out.println("Errore nella lettura file(corrotto)");
}
}
}