-1

Hello i've these 3 classes: Here i put the name of a student and the exams he gave

package traccia50719;

import java.util.*;

public class Lab {

    public static void main(String[] args) {

        Studente studente = inserimento();
        System.out.println("fine inserimento\n");
        
        studente.print();
        System.out.println("\nfine programma");
    }

    private static Studente inserimento() {
        Studente s = null;
        Esame esame=null;
        System.out.println("\nmatricola:");
        Scanner mat = new Scanner(System.in);
        int matricola= mat.nextInt();
        System.out.println("\ncognome:");
        Scanner cog = new Scanner(System.in);
        String cognome= cog.next();
        System.out.println("\nNome:");
        Scanner nom = new Scanner(System.in);
        String nome= nom.next();
        s= new Studente(matricola, cognome, nome);

        do{
            System.out.println("\ncodice esame:");
            Scanner cod = new Scanner(System.in);
            int codicesame= cod.nextInt();
            if(codicesame==0){
                break;
            }
            System.out.println("\nNome esame:");
            Scanner nomes = new Scanner(System.in);
            String nomesame= nomes.next();
            System.out.println("\nvoto esame:");
            Scanner vot = new Scanner(System.in);
            int votoesame= vot.nextInt();
            esame = new Esame(codicesame,nomesame,votoesame);
            s.addEsame(esame);
            
        }while(true);
        
        return s;
    }

}

In this class i've the student with the constructor but when i try to print exams with iterator i have only one exam printed. Why?

package traccia50719;

import java.util.*;

public class Studente {

    private int matricola;
    private String cognome;
    private String nome;
    private Set<Esame> esami = new TreeSet<Esame>();
    
    public Studente(int matricola, String cognome, String nome){
        this.matricola=matricola;
        this.cognome=cognome;
        this.nome=nome;

    }

    public void addEsame(Esame e){
        this.esami.add(e);
    }
    
    public void  print(){
        System.out.println("\nmatricola:" + this.matricola);
        System.out.println("\ncognome:" + this.cognome);
        System.out.println("\nnome:" + this.nome);
        Iterator<Esame> i = this.esami.iterator();
        while(i.hasNext()){
            Esame e = (Esame) i.next();
            e.stampaesame();
            
        }


    }
    
    
}

This is the 3 class Exam and i've stampaEsame() that print name and the result of the exam

package traccia50719;

public class Esame implements Comparable{

    private int codice;
    private String nome;
    private int voto;
    
    public Esame(int codice, String nome, int voto){
        this.codice=codice;
        this.nome=nome;
        this.voto=voto;
    }
    
    public void stampaesame(){
        System.out.println("\n nome esame:" +this.nome);
        System.out.println("\n voto esame:" +this.voto);

    }

    public boolean equals(Object o) {
        Esame esa =  (Esame) o;
        if(this.codice==esa.codice){
            return true;    
        }else return false;
        }
    

    public int compareTo(Object arg0) {
        // TODO Auto-generated method stub
        return 0;
    }
}
  • Having `compareTo` always `return 0` is not a good idea because it implies all objects are equal. Do the different `Esame` have different `codice`? – luk2302 Nov 06 '21 at 12:28
  • Does this answer your question? [TreeSet only adding one value?](https://stackoverflow.com/questions/22086153/treeset-only-adding-one-value) – luk2302 Nov 06 '21 at 12:29

1 Answers1

0

Replace the line with esami like this.

private List<Esame> esami = new List<Esame>();

If it works like this, then the problem was with the way you override the hashCode() and equals() methods for the Esami class. Due to this TreeSet is treting all of your inserts as being the same element and as a set it can only contain one instance of the same element.

Remeber, hashCode() and equals() must be overridden together ;)

Arthur Klezovich
  • 2,595
  • 1
  • 13
  • 17
  • done but don't work, i've overrided hashCode and equals :( – Fabio Palladino Nov 06 '21 at 12:34
  • public int compareTo(Object arg0) { // TODO Auto-generated method stub return 0; } Then the problem lies in this method, which says that your Esami object is equal to any object what you should create is something like this https://www.baeldung.com/java-compareto – Arthur Klezovich Nov 06 '21 at 12:36
  • @user9859592 if my answer solved your problem, please both mark it as accepted and upvote, so its easier for others to find and reuse it in their work. – Arthur Klezovich Nov 06 '21 at 13:22