• 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
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

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: 23958
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: 8957
646
Mac OS X Spring 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: 8957
646
Mac OS X Spring 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: 8957
646
Mac OS X Spring 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: 79945
396
  • 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.
     
    Won't you be my neighbor? - Fred Rogers. tiny ad:
    Gift giving made easy with the permaculture playing cards
    https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
    reply
      Bookmark Topic Watch Topic
    • New Topic