Each object of the student type previously instantiated must be inserted into a vector of objects of type Student. For this exercise you can assume that the vector has a maximum of 5 positions. To create an object of type student from the read line of the file use the split method of the String class. After loading the vector with the objects instantiated from the file, iterate through it and for each student show the name and the average of the notes in the screen.
What's missing?
public class Aluno {
protected String matricula;
protected String nome;
protected Float notaGA;
protected Float notaGB;
public Aluno(String matricula, String nome, Float notaGA, Float notaGB) {
this.matricula = matricula;
this.nome = nome;
this.notaGA = notaGA;
this.notaGB = notaGB;
}
public Aluno(String nome, String notaGA, String notaGB) {
super();
}
public String getMatricula() {
return matricula;
}
public void setMatricula(String matricula) {
this.matricula = matricula;
}
public String getNome() {
return nome;
}
public void setNome(String nome) {
this.nome = nome;
}
public Float getNotaGA() {
return notaGA;
}
public void setNotaGA(Float notaGA) {
this.notaGA = notaGA;
}
public Float getNotaGB() {
return notaGB;
}
public void setNotaGB(Float notaGB) {
this.notaGB = notaGB;
}
public String toString(){
return String.format("Matricula: %s\nNome: %\nNota GA: %\nNota GB:%s\r\n",
getMatricula(),
getNome(),
getNotaGA(),
getNotaGB());
}
}
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
public class CarregaAlunos{
protected static Aluno constroiAluno(String linha){
String atributos[] = linha.split(",");
Aluno a = new Aluno(atributos[0], atributos[1], atributos[2]);
return a;
}
public static Aluno[] carregaArquivo(String arquivo) throws IOException{
Aluno alunos[] = new Aluno[5];
FileReader fr = new FileReader(arquivo);
BufferedReader br = new BufferedReader(fr);
String linha;
int cont = 0;
while ((linha = br.readLine()) != null){
Aluno a = constroiAluno(linha);
alunos[cont] = a;
cont++;
}
br.close();
return alunos;
}
public static void main(String[] args) {
try{
Aluno a[] = carregaArquivo("alunos.txt");
for (int i=0; i < a.length; i++){
System.out.println(a[i]);
}
}
catch(IOException e){
e.printStackTrace();
}
}
}
students.txt
Alex,8.5,9.5
Paulo,9.4,10
Pedro,6.5,8.7
Ana,9.1,8.3
Carlos,7.7,8.1