• 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

integer inputs will not save apart from the first

 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have create a program that you enter student information in.  I think I have all the code right, but when I enter the informantion and then choose option "2" to print out the information entered the integer inputs all come back "0" apart from "StudentID'.  Can someone show me where I have gone wrong and what I need to do to fix it?


 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Take a look at your constructor ... and question... what happens when you have a local variable with the same name as an instance variable?

Henry
 
Marshal
Posts: 8856
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
To addition to Henry's reply, you also slipped on copy/paste error. All grades get assigned to grade1, hence overriding previous grade.
 
Liutauras Vilda
Marshal
Posts: 8856
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
In order to reduce headache thinking synonym for parameters and fields - use 'this'. i.e.:

'this' referring to an instance variable, which is on line 2.
 
David Ausere
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I see where I went wrong and got it fixed!  Thank you!

Any pointers on an easy way to make the Student ID autopopulate for each addition?
 
Liutauras Vilda
Marshal
Posts: 8856
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

David Ausere wrote:Any pointers on an easy way to make the Student ID autopopulate for each addition?

Have this field as a static and increment (within constructor) each time an object of a Student class is created.
Probably this is what your teacher is expecting at an academic level.
 
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch and well done understanding the hint about your problem.
A few style things:-
  • 1: A few of your lines are too long. Line 14 would read better as in the code below: this is how you can deal with such long lines.
  • 2: Where you have two different names for the parameter and the field in a constructor (or a set method), give them the same name, as in my version of your constructor below. That way, you can display the best name as a parameter, and use the best name for a field. Note the construct with this.xxx. That means the field called xxx rather than the local variable/parameter xxx. You may be able to improve those field names without my help.
  • 3: You have the Capitalletters in the wrong places in that constructor. Not Lastname but lastName please.
  • 4: No space after ( on line 14 please.
  • 5: There is no need for an empty line at line 24: I have removed it.
  • 6: Your getXXX methods should have a Capital Letter after get: getGrade1 rather than getgrade1 please.
  • Line 69 could also benefit from breaking into several lines.
     
    Consider Paul's rocket mass heater.
    reply
      Bookmark Topic Watch Topic
    • New Topic