aspose file tools*
The moose likes Beginning Java and the fly likes scope of variables in functions Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "scope of variables in functions" Watch "scope of variables in functions" New topic
Author

scope of variables in functions

tmwhalens
Greenhorn

Joined: Nov 04, 2001
Posts: 4
my problem is this:
i am comparing two arrays with a for loop. in each iteration, if there is a match i want to increment iCorrect by one. if there is NOT a match i want to increment iWrong by one. however, after the loops have terminated i can not access the values of variables iCorrect and iWrong. why? must i use an array for storing the number of correct and incorrect responses?
thanx,
thomas
PS. i am sticking in some code to show that i have attemtpted this problem:



[This message has been edited by tmwhalens (edited November 08, 2001).]
[This message has been edited by tmwhalens (edited November 08, 2001).]
[This message has been edited by tmwhalens (edited November 08, 2001).]
Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 5019
    
    8

Thomas,
Thanks for making the effort to format your code to make it nice and readable
I can't see anything in the code that would make it print incorrect results and my own test of this code gives me the correct results.
Some nit-picking:
- instead of naming the arrays "arrayA" and "arrayB" then adding comments about what they are, just name them "answers" and "responses". Makes the code much more readable.
- instead of hard-coding the size of the array (in this case 5, but that could change, right?), either declare a static final int NUM_QUESTIONS and use that throughout the code or just declare and initialize in the same statement, e.g.
int[] answers = {1, 2, 3, 4, 5};

- In the for-loop, it is better to use the actual array.length in the loop condition, e.g.:
for (int i = 0; i < arrayA.length; i++) { ... }
This way, if you decide to change the array size, you'll have less code to change.
Good luck!
Junilu


Junilu - [How to Ask Questions] [How to Answer Questions]
Thomas Whalen
Ranch Hand

Joined: Aug 26, 2001
Posts: 123
Junilu,
Thanks for the advice! Is it better to use the ArrayName.length property in a function << or >> use that property outside of the function? I was wondering if it might make a difference in regards to overall speed, even though speed is not going to be a factor for such a simple, small program.
Thanks,
Thomas


if you don't know, then ask. if you do know, then share. love is knowledge.
Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 5019
    
    8

Thomas,
Not sure if this is what you mean by "in" or "out" of a function but I'll take a stab:
<pre>
// is this better...
// #1
int len = array.length;
for (i = 0; i < len; i++) {...}
// ...than this?
// #2
for (i = 0; i < array.length; i++) {...}
</pre>
I don't think you gain anything with #1. Plus, you have an additional temporary variable. In his book "Refactoring", Martin Fowler often advises against using temporary variables like this and actually has a Refactoring called "Replace Temp with Query", which basically refactors #1 to #2.

------------------
Junilu Lacar
Sun Certified Programmer for the Java� 2 Platform
Thomas Whalen
Ranch Hand

Joined: Aug 26, 2001
Posts: 123
Thanks Junilu! I will work on keeping excessive temporary variables to a minimum
Thomas
William Barnes
Ranch Hand

Joined: Mar 16, 2001
Posts: 986

There is nothing wrong with the code. Try putting print statements where iCorrect and iWrong are incremented to see which block is being hit.


Please ignore post, I have no idea what I am talking about.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: scope of variables in functions