Win a copy of Modern frontends with htmx this week in the Spring forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Implementing toString with information from another class

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
   




 
Marshal
Posts: 8853
637
Mac OS X VI Editor BSD Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Saloon Keeper
Posts: 10653
85
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't see any obvious problems here. I suspect the problem is in your database code.
 
Rancher
Posts: 3324
32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Rancher
Posts: 3324
32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
Bartender
Posts: 732
10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
Marshal
Posts: 79082
376
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 }.
reply
    Bookmark Topic Watch Topic
  • New Topic