Well, I’ve developed a solution that fits more or less what you want. The solution reads a vestibular.txt file with the attributes separated by comma, then a Student object is created for each line (each student in the case) and then is saved in an Arraylist to be processed and then generated the new document, follows the code.
First we have the class Student.java, this class we will use to manipulate our student, in case order the list by grade.
Java student.
public class Aluno implements Comparable<Aluno> {
private String nome;
private String curso;
private int inscricao;
private double nota;
public String getNome() {
return nome;
}
public void setNome(String nome) {
this.nome = nome;
}
//Outros getters e setters
@Override
public int compareTo(Aluno a) {
if(this.nota > a.nota) return 1;
if(this.nota > a.nota) return -1;
return 0;
}
}
In the student class I implemented the Comparable interface, overriding the compareTo method, so when to use Collections.Sort() I can sort by note.
The student class being ready, now follows the class Processed. The class is well commented. But what it basically does: Read the vestibular.txt file and separates the attributes that are between the commas. After that a student is created to save the attributes in it and then add it to the list. After all students added to the list it is possible to do Collections.Sort, which will sort students by grade.
Then after the orderly list I made only one Foreach saving only students who have grade above 8.
Java processors.
public class ProcessadorNotas {
public static void main(String[] args) throws IOException {
Aluno aluno = null;
ArrayList<Aluno> listaDeAlunos = new ArrayList<Aluno>();
Scanner sc = new Scanner(new FileInputStream("vestibular.txt"));
while(sc.hasNextLine()) {
String linha = sc.nextLine();
//Separamos os dados por virgula, e cada campo será armazenado em uma posição da array.
String dados[] = linha.split(",");
//Criamos um objeto aluno e setamos os seus atributos para depois colarmos na lista.
aluno = new Aluno();
aluno.setNome(dados[0]);
aluno.setInscricao(Integer.parseInt(dados[1]));
aluno.setNota(Double.parseDouble(dados[2]));
aluno.setCurso(dados[3]);
//Adicionamos o aluno a lista.
listaDeAlunos.add(aluno);
}
//Vamos ordernar os alunos por notas, então iremos apenas ver quais estão acima da média na hora de escrever.
Collections.sort(listaDeAlunos);
//Agora já temos a lista de alunos, agora vamos escrever um outro arquivo com o resultado_vestibular
//Vamos supor que para ser aprovado, o aluno tem que ter nota 8.
PrintStream ps = new PrintStream("aprovados_vestibular.txt");
for(Aluno a : listaDeAlunos) {
if(a.getNota() > 8) {
ps.print(a.getNome()+",");
ps.print(String.valueOf(a.getInscricao())+",");
ps.print(String.valueOf(a.getNota())+",");
ps.print(a.getCurso());
ps.println();
}
}
ps.close();
}
}
If you have any questions put there in the comments, thanks.
So put what you did.
– Maniero
@Raulgoulart The problem you mentioned is actually quite broad and can be divided into smaller problems. I think you better start with one of them, who you’re having trouble with, and explain how you tried to attack him and what difficulties you encountered. Besides putting on what you did, like the mustache said.
– Piovezan
Divide to conquer! Ask a quick and objective question that we can answer you. See the guide [Ask]
– Math
Raul, seeing your question earlier I asked two classes that solve the problem, I will post and you check, not so agree with what the link asks. But you can look at my code and then tell me what you’re not getting, or specify what you’re not getting.
– João Neto
@bigown, posted the content I’ve made.
– Raul Goulart