1

So I have three classes:

  • Lab
  • Student
  • Exam

I need to take in input an ID of a Student, add exams of Exam class into a Treeset with Exam type, all of this inside the Student class.

After doing that, I need to print this Treeset with an Iterator.. but all I got is some weird: java.util.TreeMap$KeyIterator@1b6d3586 instead.

I created the Iterator and passed the Treeset, but the code won't work.

Here the files.

Lab.java

public class Lab {

    public static void main(String[] args) {

        Student stu = new Student();

        Student.insert();
        System.out.println("Insert ended.\n");

        stu.print();
        System.out.println("\nEnd of the program.");

    }

}

Student.java

import java.util.Iterator;
import java.util.Scanner;
import java.util.Set;
import java.util.TreeSet;

public class Student implements Comparable<Student>{

    private static int ID;

    private static Set<Exam> exams = new TreeSet<Exam>();

    public static void insert() {

        Scanner input  = new Scanner(System.in);
        Scanner input1 = new Scanner(System.in);
        Scanner input2 = new Scanner(System.in);
        Scanner input3 = new Scanner(System.in);

        int exam_code = 1;
        String exam_name;
        int exam_grade;

        System.out.println("Student's ID: ");
        ID = input.nextInt();

        System.out.println("--- EXAMS INSERT ---");

        while (exam_code != 0) {

            System.out.println("Exam's code: ");
            exam_code = input1.nextInt();

            if (exam_code == 0) {
                break;
            }

            else {

                System.out.println("Exam's name: ");
                exam_name = input2.nextLine();

                System.out.println("Exam's grade: ");
                exam_grade = input3.nextInt();

                exams.add(new Exam(exam_code, exam_name, exam_grade));

            }

        }

        input.close();
        input1.close();
        input2.close();
        input3.close();

    }

    public void print() {

        System.out.println("ID: " + ID);

        System.out.println("\nExams done: ");

        Iterator<Exam> es = exams.iterator();

        boolean hasnext = false;

        while (hasnext != true) {

            if (es.hasNext()) {

                System.out.println(es);

                hasnext = true;
            }

            else {
                hasnext = false;
            }
        }

    }

    @Override
    public int compareTo(Student o) {
        // TODO Auto-generated method stub
        return 0;
    }

}

Exam.java


public class Exam implements Comparable<Exam>{

    public int exam_code;
    private String exam_name;
    private int exam_grade;

    public Exam(int code, String name, int grade) {

        exam_code = code;
        exam_name = name;
        exam_grade = grade;

    }

    @Override
    public int compareTo(Exam o) {
        // TODO Auto-generated method stub
        return 0;
    }

}
SlimShadys
  • 119
  • 3
  • 17
  • Does this answer your question? [How do I print my Java object without getting "SomeType@2f92e0f4"?](https://stackoverflow.com/questions/29140402/how-do-i-print-my-java-object-without-getting-sometype2f92e0f4) – azurefrog Jan 16 '20 at 22:34
  • Unfortunately, it prints the same thing. :( – SlimShadys Jan 16 '20 at 23:15

1 Answers1

2

Replace

System.out.println(es);

to

System.out.println(es.next());

EDIT: Better replace the whole print() method to

public void print() {

        System.out.println("ID: " + ID);

        System.out.println("\nExams done: ");

        for (Exam exam : exams) {
            System.out.println(exam);
        }

    }

Because you don't need to implement iteration by yourself, there is for-each loop in Java for such cases.

EDIT: Using Iterator

public void print() {

    System.out.println("ID: " + ID);

    System.out.println("\nExams done: ");

    Iterator<Exam> es = exams.iterator();

    while (es.hasNext()) {
        System.out.println(es.next());
    }
}
ardenit
  • 3,610
  • 8
  • 16
  • Your new print() method works, but I don't know why, only prints the first exam I add. The others are discarded. Plus, my teacher told us to print the Treeset with an Iterator, that's why I implemented it. – SlimShadys Jan 16 '20 at 23:14
  • 1
    Ah, there was a mistake in my code. Edited it and also added a proper implementation using Iterator – ardenit Jan 16 '20 at 23:19
  • Okay that print() method with Iterator implementation works, but it looks like it just stores one exam, since it only prints one if I add multiple exams. Maybe and error at the ```exams.add``` method? – SlimShadys Jan 16 '20 at 23:23
  • 1
    Yes, there is another problem. You use TreeSet to store your Exam instances, and TreeSet requires Exam to implement Comparable interface and implement compareTo method, but your implementation is incorrect (it just returns 0, that means that TreeSet thinks that all your Exam instances are equal, and Set does not store duplicate instances, so it will only store one of them). Please read https://docs.oracle.com/javase/8/docs/api/java/lang/Comparable.html and properly override compareTo() and equals() methods in your Exam class. – ardenit Jan 16 '20 at 23:32
  • 1
    If you are struggling with creating a natural order for Exam instances, you can use HashMap instead of TreeMap. This way you don't have to implement Comparable interface, but you have to properly override hashCode() and equals() methods – ardenit Jan 16 '20 at 23:35
  • Okay thank you for your kind help. Appreciate that a lot. I will try and let you know. – SlimShadys Jan 16 '20 at 23:38
  • Or, the easiest option, you can use HashMap instead of TreeMap and override nothing, if you want to store in set all instances of Exam, even if some of them have equal exam_code, exam_name, and exam_grade field values. – ardenit Jan 16 '20 at 23:38
  • 1
    Implemented the compareTo() method accordingly and now it prints the Treeset. Thank you :) – SlimShadys Jan 17 '20 at 16:59