简体   繁体   中英

Cant figure out how display array of objects Java

public class CollegeCourse
{

   private String courseID;
   private int creditHours;
   private char grade;

   public String getCourse()
   {
      return courseID;
   }

   public int getCredits()
   {
      return creditHours;
   }

   public char getGrade()
   {
      return grade;
   }

   public void setCourse(String cid)
   {
      courseID=cid;
   }

   public void setCredits(int hours)
   {
      creditHours=hours;
   }
   public void setGrade(char g)
   {
      grade=g;
   }
}


public class Student
{

   private int studentID;
   private CollegeCourse[] classes=new CollegeCourse[5];

   public int getID()
   {
      return studentID;
   }
   public void setStudentID(int s)
   {
      studentID=s;
   }

   public CollegeCourse getCourses(int x)
   {
      CollegeCourse course=classes[x];
      return course;
   }

   public void setObject(CollegeCourse obj,int x)
   {
      classes[x]=obj;
   }
}








import java.util.Scanner;

public class InputGrades
{
   public static void main(String[] args)
   {
      String course;
      int credits;
      char grade;
      Student[] grades=new Student[10];
      Scanner input=new Scanner(System.in);

      for (int x=0;x<1;x++)
      {  
         grades[x]=new Student();
         System.out.println("Enter ID for student #"+(x+1));
         grades[x].setStudentID(input.nextInt());
         for(int y=0;y<5;y++)
         {
            CollegeCourse one=new CollegeCourse();

            input.nextLine();

            System.out.println("Enter course ID for class number "+y); 
            one.setCourse(input.nextLine());

            System.out.println("Enter credits for "+y);
            one.setCredits(input.nextInt());

            input.nextLine();
            System.out.println("Enter a grade for "+y);
            one.setGrade(input.nextLine().charAt(0));

            grades[x].setObject(one, y);
         }
      }

      for (int i=0;i<1;i++)
      {
         for (int j=0;j<5;j++)
         {
            System.out.println("Course: "+grades[i].getCourses(j));
           //Cant figure out how to print this
         }
      }
   }
}

It will print the student ID number and that is it. It then prints out a bunch of garbage for the remaining courses displayed under the "j" for loop. I cant seem to figure it out... Any help would be appreciated

You are printing the reference to the CollegeCourse object at the specified index.

You can either write a toString() method for CollegeCourse to print out the attributes you are interested in OR you can alter your existing line:

System.out.println("Course: "+grades[i].getCourses(j).getCourse());

I'll bet that "bunch of garbage" is the object reference for the array or the object it contains.

Your object needs to override toString() and the container needs to loop over the contents.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM