3
Each previously instantiated student type object must be inserted into a Student type object array. For this exercise you can assume that the vector has a maximum of 5 positions. To create an object of the student type from the read line of the file use the split method of the String class. After loading the vector with the instantiated objects from the file, scroll through it and for each student show on the screen the name and average of the notes.
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();
}
}
}
txt students.
Alex,8.5,9.5
Paulo,9.4,10
Pedro,6.5,8.7
Ana,9.1,8.3
Carlos,7.7,8.1
Welcome! Please, we suggest you perform a tour by the site and see How to create a Minimum, Complete and Verifiable example! So make it easier for the community to help you!
– Thiago Luiz Domacoski
Can add the file txt students.??
– Thiago Luiz Domacoski
I posted below the class Loading students
– Ricardo