Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Implementing toString with information from another class  RSS feed

 
Brandon Williams
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For this homework, we have to implement a HashMap database.  When I run the program, I get a message dialog showing the following:  I believe I'm having an issue bringing the information to my toString method in my Student class, but I can't find the error.
   
    Name: Name:, Major: Major:, GPA: 4.0
   
    Anyone know what am I doing wrong here?
   




 
Liutauras Vilda
Sheriff
Posts: 4917
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't think you should have some logic within toString() method. I mean if statements or anything similar.
toString() method supposed to print field/-s values of a given time when the method is invoked.

I don't think this is the cause of this issue you are facing, but first I'd fix that and look further what the issue might be.
 
Carey Brown
Saloon Keeper
Posts: 3318
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't see any obvious problems here. I suspect the problem is in your database code.
 
Rob Camick
Ranch Hand
Posts: 2828
15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I believe I'm having an issue bringing the information to my toString method in my Student class


What is with all the code???

If you want to test a method of a class then all you need to do is create an instance of the class and then invoke the method of the class.

It works fine for me:

 
Brandon Williams
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The problem wasn't with the code compiling, rather it was with my toString not having the correct information in it.  Anyways, I figured it out.  I had my name_texy pointed to my JLabel instead of the text box.  Thanks all!

Rob Camick wrote:
I believe I'm having an issue bringing the information to my toString method in my Student class


What is with all the code???

If you want to test a method of a class then all you need to do is create an instance of the class and then invoke the method of the class.

It works fine for me:

 
Rob Camick
Ranch Hand
Posts: 2828
15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The problem wasn't with the code compiling,


I never said it was.

rather it was with my toString not having the correct information in it.


Exactly and I showed you how to debug your code in a couple of statements instead of "guessing" that the problem was with the toString() method.

The point is to learn to simply the problem so you can concentrate on finding the problem instead of guessing.

Did you run the code I provided for you?

If you did you would know the problem is not with the toString() method, but with how you create the Student class. This will allow you to narrow done where in the code to look for the problem!
 
Fred Kleinschmidt
Bartender
Posts: 571
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You need to do some serious re-design. As your code now stands, a Student can take at most one course, and you have no way of specifying what course that is!
 
Campbell Ritchie
Marshal
Posts: 56541
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Consider replacing most of line 137‑157 with a switch statement.
Please reconsider the position of your empty lines. One empty line between successive methods, please, not an empty line before the }.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!