Two Laptop Bag*
The moose likes Beginning Java and the fly likes problem with initializing Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "problem with initializing" Watch "problem with initializing" New topic
Author

problem with initializing

Vee Shall
Greenhorn

Joined: Apr 03, 2010
Posts: 7
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.







Being a scholar of life does not require a student loan.
Bert Wilkinson
Ranch Hand

Joined: Oct 28, 2009
Posts: 33
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

Joined: Apr 03, 2010
Posts: 7
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
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10170
    
    8

Vee Shall wrote:

Please check your private messages for an important administrative matter


[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10170
    
    8

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

Joined: Oct 28, 2009
Posts: 33
oops. Missed that!

Got carried away with the claims of bad output when the code doesn't even compile....harrumph.
Vee Shall
Greenhorn

Joined: Apr 03, 2010
Posts: 7
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?
Muneeswaran Balasubramanian
Ranch Hand

Joined: Mar 19, 2010
Posts: 138
If you do that you have to use looping statements.It reduce the line of codes and make it more readable one.
Cheers Munees


Cheers Munees
My Blog
 
 
subject: problem with initializing
 
Similar Threads
Passing information to a class that displays a GUI.
Compile error
TestScores
TestScores
Multi dimensional arrays