my dog learned polymorphism*
The moose likes Beginning Java and the fly likes array variables out of scope-why? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "array variables out of scope-why?" Watch "array variables out of scope-why?" New topic
Author

array variables out of scope-why?

mike hengst
Ranch Hand

Joined: Oct 19, 2002
Posts: 43
I am trying to write this class called GradePoint. For some reason I am not seeing why these few variable,which come from an array, are not being seen by the method setGrade. If anyone can tell me why this is happening I would be very pleased. This is a school project that I have out a lot of time into and would like a little help figuring out what is left to do. GradePoint is called by another class. Thanks for the time...
here is the code


Thank you Oh Lord<br />For the white blind light<br />A city rises from the sea<br />I had a splitting headache<br />from which the future's made<br />--morrisson
James Chegwidden
Author
Ranch Hand

Joined: Oct 06, 2002
Posts: 201
What does setGrade look like?


Author and Instructor, my book
James Chegwidden
Author
Ranch Hand

Joined: Oct 06, 2002
Posts: 201
Also, why do you have a method that just creates a Student()? Why not take it out of the method?
James Chegwidden
Author
Ranch Hand

Joined: Oct 06, 2002
Posts: 201
Also, would you have to create the Student object first then- create the Array of student objects.
Marilyn de Queiroz
Sheriff

Joined: Jul 22, 2000
Posts: 9044
    
  10
James, see this post for more details on this.


JavaBeginnersFaq
"Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt
Marilyn de Queiroz
Sheriff

Joined: Jul 22, 2000
Posts: 9044
    
  10
I'm sorry I haven't had time to look into this for you. Unfortunately, I have a meeting after work today also, so it will be at least another day before I'll be able to try to help.
mike hengst
Ranch Hand

Joined: Oct 19, 2002
Posts: 43
For the method that create the Student objects it is one that is required in my specs. setGrade is as follows. It may not be entirely correct either.
William Barnes
Ranch Hand

Joined: Mar 16, 2001
Posts: 984

It is a little hard to tell what is happening here as atleast two of the arrays are from somewhere else.
But this I am able to see: your 'for' loop is assinging different values to the same variables. Which doesn't make much sense to me. So for example 'gradePoints[index]' is being set to a different value each time the loop runs. And will end up containing the value of 'points[i]', where 'i' is '< studentGrades.length'.


Please ignore post, I have no idea what I am talking about.
William Barnes
Ranch Hand

Joined: Mar 16, 2001
Posts: 984

Another problem is that unless all your arrays are the exact same length, there is a chance you will go out of bounds.
In the 'for' loop you are using the length of 'studentGrades' to iterate through 2 others arrays. Not a safe thing to do.
mike hengst
Ranch Hand

Joined: Oct 19, 2002
Posts: 43
Originally posted by William Barnes:
It is a little hard to tell what is happening here as atleast two of the arrays are from somewhere else.
But this I am able to see: your 'for' loop is assinging different values to the same variables. Which doesn't make much sense to me. So for example 'gradePoints[index]' is being set to a different value each time the loop runs. And will end up containing the value of 'points[i]', where 'i' is '< studentGrades.length'.

good point here and I thank you. I had not seen that yet. I put a break; statement in. That should stop the loop when the index of the letterGrade is the same as the gradePoint index. Is that correct? With your second comment. In this case the arrays are the same length throughout. This is just an exercise. Thanks for your time any further comments are appreciated.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: array variables out of scope-why?
 
Similar Threads
not a statement
to Dave Lander,Marilyn de Queiroz
java error
Why can student[x] be resolved?
Error in calculation