2

First Day with Java and I am getting an error in the for loop when I add the array index to the variable minIndex. I'm not sure exactly what to put in as the value in the (). I tried i but that didn't work and with my lack of knowledge in java, I am unsure. May I have some guidance please.

public static int minPosition(double[] list) {
     double leastNum;
     leastNum = list[0];
     // leastNum starts at first number in array
     int minIndex;
     minIndex = 1;
     // minIndex starts at 1 as if first number in Array happens to be the lowest, its index will be one
     for ( int i = 0; i < list.length; i++)
         if (list[i] < leastNum)
             leastNum = list[i];
             minIndex = list.indexof(i);
     return minIndex;
1
  • You need braces for the block of statements in your if statement Commented Apr 6, 2015 at 4:05

2 Answers 2

1

i is the index.

change

minIndex = list.indexof(i);

to

minIndex = i;

You should also change

minIndex = 1;

to

minIndex = 0;

since the first index of an array is 0.

As commented, you have some missing curly braces. Here's the full code :

public static int minPosition(double[] list) {
     double leastNum = list[0];
     // leastNum starts at first number in array
     int minIndex = 0;
     // minIndex starts at 0 as if first number in Array happens to be the lowest, its index will be one
     for ( int i = 0; i < list.length; i++) // you can also start with i = 1
                                            // since you initialize leastNum
                                            // with the first element
         if (list[i] < leastNum) {
             leastNum = list[i];
             minIndex = i;
         }
     return minIndex;
}
Sign up to request clarification or add additional context in comments.

4 Comments

He also needs a block around the if statement. (Adding one around the for statement would be good style as well)
The minIndex logic looks good as is. It is the initial index of the mininum value in the array. No need to compare it to itself.
thank you @Eran here is what I did, but currently I still have an error the error states "i cannot be resolved to a varriable" can you explain please
@scottb since leastNumis initialized to list[0], minIndex should be initialized to 0. I agree, though, that i can start at 1 instead of 0.
0

There is no such method in primitive array indexof():

public static int minPosition(double[] list) {
     int minIndex = 0;//replaced
     double leastNum = list[minIndex];//replaced
     for ( int i = 1; i < list.length; i++) {//braces recommended, also `i` starts with `1`
         if (list[i] < leastNum) {//braces required, since you have more than one line to execute
             leastNum = list[i];
             minIndex = i;//replaced
         }
     }
     return minIndex;
}

1 Comment

ok this makes alot of sense . And now I fully understand the braces. thank you

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.