Java Text Streams

Asked

Viewed 251 times

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

1 answer

1


Your code became very good, only lacked small details. I searched here and was adapting.

First problem: The file path has to be complete. I put the file in c: and the file name I passed "C:\\alunos.txt" (note that there are two bars for one to escape the other and the java do not think I want to escape the letter 'a' of students) and stopped giving exception saying that could not find the file.

Second problem: I did not understand why it called super in the second constructor instead of doing as was done the first. super is used to call methods of a parent class, as its class does not extend any other, I did not understand the 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. Was corrected:

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

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

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

Follow the working modified files:

Java student.

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());
    }

}

Load students.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 ask in the comments. If this answer has helped you, give a moral mark as accepted answer and click the arrow up to give me reputation points.

  • Thanks for the help!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.