• Post Reply Bookmark Topic Watch Topic
  • New Topic

ArryayList not printing right  RSS feed

 
Cody Biggs
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So I have been looking everywhere for an answer to this, and im still confused as when I started. I have two array lists one for courses, and one for majors. All im trying to do is print the entire list of each arraylist, but my major array lists does not print right. I was told that a toString method is the way to fix it, but we have not gone over toString methods yet. I saw some examples of the toString but those were with just one class. So here is what I have

MainClass:



My Course Class:



My Major Class.



My catalog class where I load and print the lists



And the output where my major lists doesnt even show up



 
Norm Radder
Rancher
Posts: 2240
28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
edu.tridenttech.cpt237.biggs.neptune.Major@3af49f1c

That is the String returned by the default toString() method from the Object class. If you want to see something different, override the toString() method in the Major class and return the String you want to see.

See the tutorial: http://docs.oracle.com/javase/tutorial/java/IandI/override.html

Note: When you override a method, you should code @Override before the method's definition to allow the compiler to check that the method's signature has been correctly coded.

Also posted at: http://www.dreamincode.net/forums/topic/394600-problem-printing-array-list/
 
Cody Biggs
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Right I did post over there as well, but it would not let me reply. I got the whole @override. My problem is I was told that it needs to go into the major class, not the catalog where my list is. So im a making a new object in the toString to call my list? As far as I have looked I have come up with is this, being in my major class

 
Carey Brown
Saloon Keeper
Posts: 3323
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Some notes...

class Course
Line 10 you are not using parameters.
Need a toString() method.

class Major
"catalog1" not used.
Line 10 & 11: swapped variables.
Line 19: convention is to name boolean getters like "isDegree".
Line 31: you are returning a collection so it should be "getRequiredCourses" (plural).
Line 6: shouldn't this be a List<Course> ?

class Catalog
Line ?: your getCourseList() should only return the courses. Do the printing in printCourseList().
Line ?: ditto getMajorList().
 
Carey Brown
Saloon Keeper
Posts: 3323
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Cody Biggs wrote:Right I did post over there as well, but it would not let me reply. I got the whole @override. My problem is I was told that it needs to go into the major class, not the catalog where my list is. So im a making a new object in the toString to call my list? As far as I have looked I have come up with is this, being in my major class


I'd start out with something simple, like:
 
Cody Biggs
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Now I understand! im glad you pointed out that i had my name and isDegree backwards with majorName and degree. I originally tried returning just name, but nothing would ever show up. Now I see why. Thank you! As for the rest of it I do need to clean it up and delete some stuff.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!