Java Text Streams

3

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
    
asked by anonymous 15.03.2017 / 21:04

1 answer

1

Your code was very good, just missing small details. I turned here and adapted.

First problem: The file path must be complete. I put the file in c: \ and the filename passed "C:\alunos.txt" (notice that there are two slashes to escape the other and java does not think I want to escape the letter 'a' from students) and stopped giving exception saying which could not find the file.

Second problem: I did not understand why it called super in the second constructor instead of doing the first one. super is used to call methods of a parent class, as its class does not extend any other, I did not understand super (). So I changed the super () to:

    this.nome = nome;
    this.notaGA = Float.parseFloat(notaGA);
    this.notaGB = Float.parseFloat(notaGB);   

Third problem: I found it interesting that you used String.format but you ate some s where it should be %s . Fixed:

Matricula: %s\nNome: %s\nNota GA: %s\nNota GB:%s\r\n

At the suggestion of IDE I changed the iteration of the array to a foreach:

     for (Aluno a1 : a)
     {
         System.out.println(a1);
     }

Here are the modified files working:

Student.java

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(); // ?

        this.nome = nome;
        this.notaGA = Float.parseFloat(notaGA);
        this.notaGB = Float.parseFloat(notaGB);   
    }


    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: %s\nNota GA: %s\nNota GB:%s\r\n", 
                getMatricula(),
                getNome(),
                getNotaGA(),
                getNotaGB());
    }

}

UploadAlumni.java

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("C:\alunos.txt");

            for (Aluno a1 : a)
            {
                System.out.println(a1);
            }

        }
        catch(IOException e)
        {
            e.printStackTrace();
        }

    }

}

Any questions please ask in the comments. If that answer has helped you, give it a moral by marking it as an accepted response and by clicking on the little box above to give me reputation points.

    
16.03.2017 / 02:18