-1

I was working on a problem for a club and tried to run it, here is the code:

package week23.UniversityProblem;

import java.util.ArrayList;
import java.util.Scanner;

public class UniversityProblem {
    public static void main(String[] args) {
        Lesson math101 = new Lesson();
        math101.name = "Mathematics";
        math101.credit = 4;
        Lesson phys101 = new Lesson();
        phys101.name = "Physics";
        phys101.credit = 3;
        Lesson cs102 = new Lesson();
        cs102.name = "Computer Science";
        cs102.credit = 3;
        Lesson ictapp = new Lesson();
        ictapp.name = "Ict applications";
        ictapp.credit = 3;
        Lesson rs101 = new Lesson();
        rs101.name = "Romainian Studies";
        rs101.credit = 3;
        int o = 0;
        Scanner scanner = new Scanner(System.in);
        Scanner scanner2 = new Scanner(System.in);
        while (o != 2){
            System.out.println("What option do you want to use:");
            System.out.println("1. Add & Print");
            System.out.println("2. Exit");
            o = scanner.nextInt();
            if(o == 1){
                Student s = new Student();
                System.out.println("Name:");
                s.name = scanner2.nextLine();
                int c = 0;
                int o2 = 0;
                while(c < s.maxCredit){
                    System.out.println("What course would you like to take:");
                    System.out.println("1. " + math101.name);
                    System.out.println("2. " + phys101.name);
                    System.out.println("3. " + cs102.name);
                    System.out.println("4. " + ictapp.name);
                    System.out.println("5. " + rs101.name);
                    System.out.println("6. Exit");
                    o2 = scanner.nextInt();
                    if(o2 == 1){
                      s.arr2.add(math101);
                      c += math101.credit;
                    } else if (o2 == 2) {
                        s.arr2.add(phys101);
                        c += phys101.credit;
                    } else if (o2 == 3) {
                        s.arr2.add(cs102);
                        c += cs102.credit;
                    } else if (o2 == 4) {
                        s.arr2.add(ictapp);
                        c += ictapp.credit;
                    } else if (o2 == 5) {
                        s.arr2.add(rs101);
                        c += rs101.credit;
                    } else if (o2 == 6) {
                        break;
                    }
                }
                System.out.println(s.name + " ");
                for (int i = 0; i < s.arr2.size(); i++) {
                    System.out.println(s.arr2.get(i));
                }
            }
        }
    }
}

And here is my lesson class and it's attributes:

package week23.UniversityProblem;

import java.util.ArrayList;

public class Lesson {
    String name;
    int credit;
}

And here is my Student class and it's attributes. Also, my arraylist is in the Student class. All of my lessons are stored in this arraylist. At the end I want to display student name and his/her lesson names with credits

package week23.UniversityProblem;

import java.util.ArrayList;

public class Student {
    String name;
    int maxCredit = 10;
    ArrayList<Lesson> arr2 = new ArrayList<>();
}

After choosing my options from the menu i added, here is what it displays:

Tudor
week23.UniversityProblem.Lesson@77459877
week23.UniversityProblem.Lesson@5b2133b1
  • This is the "memory address" of that instance of `week23.UniversityProblem.Lesson` and is the default behavior of calling `.ToString()`. If you override the `public String toString()` in your `Lesson` class, you can change what it will say. – nbokmans Jun 30 '22 at 11:43
  • Try this instead: `System.out.println(s.name + " "); System.out.println("Courses:\n========"); for (Lesson lesson : s.arr2) { System.out.println(" Name: " + lesson.name); System.out.println(" Credit: " + lesson.credit); System.out.println("--------------------------\n"); }` – DevilsHnd - 退職した Jun 30 '22 at 12:49

2 Answers2

0

The Lesson class does not override the toString() method provided by the Object class (which all objects extend directly if they are not declared to extend some other object). As such, the JRE will use the toString() method of the Object class. The javadoc for that implementation states that this will be

getClass().getName() + '@' + Integer.toHexString(hashCode())
vsfDawg
  • 1,425
  • 1
  • 9
  • 12
0

The result you get in your console is the hashcode value of an object. Since the object has no toString method to determine how it should be printed as a String. It instead prints the hashcode value which is the output you are receiving.

There are multiple ways to solve this issue. Here is one example: Add a toString method to your Lesson class:

 public String toString(){//overriding the toString() method  
  return "Name: "+name+", Credit: "+credit;  
 } 
Choppa
  • 31
  • 7