-
December 7th, 2011, 11:03 AM
#1
Need help with arrays
So im trying to find the min number in an array. Seems simple but I keep getting an error
Here is my code.
private int findMin(int numbers[]) {
//PRE numbers.length >= 1
int min = numbers [0];
for (int i = 0; i > numbers.length; i++); {
if (numbers [i] < min) {
min = numbers [i];
}
}
return min;
}
I keep getting the error (cannot find symbol - variable i). Am I missing something. Would greatly appreciate any help
-
December 7th, 2011, 11:11 AM
#2
Re: Need help with arrays
I think I might have figured it out. Am I just missing a () after numbers.length?
My second question though is, is this how you build an array? I dont have any notes on it or could I find any good tuts online.
private int[] buildArray(String line) {
int[]arr = new int[line.length()];
arr[0] = 0;
for (int i = 1; i < line.length(); i++){
arr[i] = Character.digit(line.charAt(i),10);
}
return arr;
}
-
December 7th, 2011, 11:18 AM
#3
Re: Need help with arrays
Please use code tags when posting code
I think I might have figured it out. Am I just missing a () after numbers.length?
Don't just guess an answer and ask us to check if it is correct. Does it now compile and run, if so you have figured it out?
Hint: The answer is NO.
'i' is declared in the for statement and so is only visible in the for statement and its code block. However you have put a semi-colon after the for loop which terminates the code black and so the code following the semi-colon has nothing to do with the for loop.
BTW you may want to consider if you really want the for loop to continue whilst 'i' is greater than the length of the number array.
My second question though is, is this how you build an array?
Again, it is up to you to compile it, run it and see what the output is. If you can't get it to work feel free to ask for help.
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|