• Post Reply Bookmark Topic Watch Topic
  • New Topic

integer inputs will not save apart from the first  RSS feed

 
David Ausere
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • 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?


 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • 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
 
Liutauras Vilda
Sheriff
Posts: 4923
334
BSD
  • Mark post as helpful
  • send pies
  • 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
Sheriff
Posts: 4923
334
BSD
  • Mark post as helpful
  • send pies
  • 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
  • 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
Sheriff
Posts: 4923
334
BSD
  • Mark post as helpful
  • send pies
  • 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.
 
Campbell Ritchie
Marshal
Posts: 56570
172
  • Mark post as helpful
  • send pies
  • 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.
     
    With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!