• 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

problem with initializing

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
New to the forum - go easy on me

I am dealing with aggregates and i have created a program that will ask for user input to determine a student average. I ask the user to enter test score 1,2 and 3 for student number one, and then ask teh same info for student number 2. the problem is that i call teh variable testScore1,testScore2,testScore3. As a result, student 2 will output the same grade values as student one. I believe teh problem is that Java has no way to know that once student one's average is calculated taht it must set the counter back to zero and now use new values to calculate the average for student 2. I'm not sure how to do this wehn i've used a constructor to initialize the testScores to zero.

Here is my code.





 
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Aside from some minor formatting and style issues, your code looks correct.

What's happening when you execute it and put in different values? Your testScore variables are independent for each student object that you create (inside the student object), so there shouldn't be any problem with that. As far as the average, there is no "counter" it just returns a computation based on the variables that are, again, independent from other students within your student object. I don't see any showstoppers on first pass... ???
 
Vee Shall
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i don't have any opportunity to enter a value for the second student (marsha) because i get compile errors. The program is not recognizing the difference between
John's testScore1 and Marsha's testScore1. When i try to compile it's saying that its already defined most likely because we already referenced these score values when we got John's
average.


StudentBody.java:54: testScore1 is already defined in main(java.lang.String[])
int testScore1 = scan.nextInt();
^
StudentBody.java:56: testScore2 is already defined in main(java.lang.String[])
int testScore2 = scan.nextInt();
^
StudentBody.java:58: testScore3 is already defined in main(java.lang.String[])
int testScore3 = scan.nextInt();
^
these compile errors are happening at the point where i request input for the second students scores
 
Bartender
Posts: 11497
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Vee Shall wrote:


Please check your private messages for an important administrative matter
 
Maneesh Godbole
Bartender
Posts: 11497
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can only have uniquely named variables. However, you can reassign values to a variables once its declared.
Just switch

to

Similarly for testScore2 and testScore3.
 
Bert Wilkinson
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
oops. Missed that!

Got carried away with the claims of bad output when the code doesn't even compile....harrumph.
 
Vee Shall
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hoo haa!! that worked.......
but just so i'm clear on how this is working............if i added on to this program to allow averages for 5 students isntead of 2 , would this line of code " testScore1 = scan.nextInt(); "
allow me to pass a new value to the test score?
 
Ranch Hand
Posts: 138
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you do that you have to use looping statements.It reduce the line of codes and make it more readable one.
Cheers Munees
 
Fire me boy! Cool, soothing, shameless self promotion:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic