Home > Software design >  Hashset iterator print
Hashset iterator print

Time:11-14

Lab Class

import java.math.BigInteger;
import java.util.Scanner;

public class Lab {

    public static void main(String[] args) {

        Studente s;
        inserimento();
    }

    public static void inserimento() {

        Studente s = null;
        do {
            try {
                //inserimento matricola
                System.out.println("\nmatricola:");
                Scanner mat = new Scanner(System.in);
                String matrstring = mat.nextLine();
                if (matrstring.equals("")) {
                    break;
                }
                int matricola = Integer.parseInt(matrstring);

                //inserimento cognome
                System.out.println("\ncognome:");
                Scanner cog = new Scanner(System.in);
                String cognome = cog.next();

                //inserimento nome
                System.out.println("\nnome:");
                Scanner nom = new Scanner(System.in);
                String nome = nom.next();

                //caricamento studente
                s = new Studente(matricola, cognome, nome);
                //caricamento studenti nell'hashset
                s.addStudenteSet(s);

            } catch (Exception e) {
                System.out.println("Dati inseriti sbagliati");
            }
        } while (true);

        System.out.println("fine inserimento");

        s.print();

    }

}

In this class i take in input the student code, surname and name and i put them into Studente class.

import java.util.*;


public class Studente {

    private int matricola;
    private String cognome;
    private String nome;
    private Set<Studente> studenti = new HashSet<Studente>();

    public Studente(int matricola, String cognome, String nome)  {
        
        this.matricola=matricola;
        this.cognome=cognome;
        this.nome=nome;
    }
    
    public void addStudenteSet(Studente s){
        this.studenti.add(s);
    }
    @Override
    public boolean equals(Object o){
        Studente st = (Studente) o;
        if(this.matricola==st.matricola){
            return true;
        }else return false; 
    }
    @Override
    public int hashCode(){
        return Integer.hashCode(matricola);
    }
    
    
    public void print(){
        Iterator<Studente> i = this.studenti.iterator();
        while(i.hasNext()){
            Studente student = i.next();
            System.out.println("matricola: "   student.matricola   "\ncognome: "  student.cognome  "\nnome: "  student.nome);

        }
        
    }
}

Here i used an hashset and into print method i want to print every student i took into lab class, but it prints only the last one. How can i resolve this problem? Into the Lab class i called the method addStudenteSet(s);

CodePudding user response:

There is a problem with following lines.

s = new Studente(matricola, cognome, nome);
//caricamento studenti nell'hashset
s.addStudenteSet(s);

you are creating a new object "s" and adding "s" into the HashSet inside that object. So each iteration will make new object and the HashSet will be the same for all objects.

you can make HashSet studenti as static then it will same for all objects.

private static Set studenti = new HashSet();

or you can check the following code(Please note i only consider the issue that you mentioned, not fixed any other implemention issues).

public class Lab {

    private static  Set<Studente> studenti = new HashSet<Studente>();

    public static void main(String[] args) {

        Studente s;
        inserimento();
    }

    public static void inserimento() {

        Scanner mat = new Scanner(System.in);
        Studente s = null;
        boolean isContinue=true;
        do {
            try {
                //inserimento matricola
                System.out.println("\nmatricola:");
                String matrstring = mat.nextLine();
                if (matrstring.equals("")) {
                    break;
                }
                int matricola = Integer.parseInt(matrstring);

                //inserimento cognome
                System.out.println("\ncognome:");
                String cognome = mat.nextLine();

                //inserimento nome
                System.out.println("\nnome:");
                String nome = mat.nextLine();

                //caricamento studente
                s = new Studente(matricola, cognome, nome);
                //caricamento studenti nell'hashset
                studenti.add(s);
                
                System.out.println("\nenter 1 to continue:");
                isContinue= Integer.parseInt(mat.nextLine())==1 ? true :false;
            } catch (Exception e) {
                System.out.println("Dati inseriti sbagliati");
                isContinue=false;
            }
        } while (isContinue);

        System.out.println("fine inserimento");

        print();

    }



 public static void print(){
        Iterator<Studente> i = studenti.iterator();
        while(i.hasNext()){
            Studente student = i.next();
            System.out.println("matricola: "   student.getMatricola()   "\tcognome: "  student.getCognome()  "\tnome: "  student.getNome() "\n");

        }
        
    }

}



public class Studente {

    private int matricola;
    private String cognome;
    private String nome;
    
    public int getMatricola() {
        return matricola;
    }

    public String getCognome() {
        return cognome;
    }

    public String getNome() {
        return nome;
    }
    public Studente(int matricola, String cognome, String nome)  {
            
        this.matricola=matricola;
        this.cognome=cognome;
        this.nome=nome;
    }

    @Override
    public boolean equals(Object o){
        Studente st = (Studente) o;
        if(this.matricola==st.matricola){
            return true;
        }else return false; 
    }
    @Override
    public int hashCode(){
        return Integer.hashCode(matricola);
    }
}

CodePudding user response:

Each Studente has its own Set<Studente> that only ever contains a single Studente (namely the current studente itself because you create a new studente with s = new Studente(matricola, cognome, nome); and then add it to its own set with s.addStudenteSet(s);).

Instead of this you need one Set<Studente> within your void inserimento() method:

public static void inserimento() {

    Scanner scanner = new Scanner(System.in);
    Set<Studente> studenti = new HashSet<Studente>();
    do {
        try {
            //inserimento matricola
            System.out.println("\nmatricola:");
            String matrstring = scanner.nextLine();
            if (matrstring.equals("")) {
                break;
            }
            int matricola = Integer.parseInt(matrstring);

            //inserimento cognome
            System.out.println("\ncognome:");
            String cognome = scanner.next();

            //inserimento nome
            System.out.println("\nnome:");
            String nome = scanner.next();

            //caricamento studente
            s = new Studente(matricola, cognome, nome);
            //caricamento studenti nell'hashset
            studenti.add(s);
            scanner.nextLine(); // added to skip the pending newline after scanner.next();

        } catch (Exception e) {
            System.out.println("Dati inseriti sbagliati");
        }
    } while (true);

    System.out.println("fine inserimento");

    for (Studente s: studenti) {
        System.out.println("matricola: "   student.matricola   "\ncognome: "  student.cognome  "\nnome: "  student.nome);
    }

}
  • Related