How to find the array index of the lowest value in the array?

• November 28th, 2013, 09:30 PM
namenamename
How to find the array index of the lowest value in the array?
Code :

import java.util.Scanner;

class array2{
public static void main(String args[]){
Scanner input = new Scanner(System.in);

int values;
int num[] = new int[10];
System.out.println("Enter 10 values: ");
for(int i=0;i<num.length;i++){
num[i] = input.nextInt();
System.out.print(num[i]+" ");
}
int sum = 0;
for(int j : num){
sum += j;
}
System.out.print("\nThe average is "+sum/10);

int smallest = num[0];
int largest = num[0];

for(int i=1; i< num.length; i++){
if(num[i] > largest){
largest = num[i];
}else if (num[i] < smallest){
smallest = num[i];
}
}
int indexs = smallest.indexOf(smallest);
System.out.println("\nindex of smallest number(s) is "+indexs);

System.out.println("\nSmallest Number is " + smallest);

}
}

I need to get the index position of the lowest values in the array, but the line "int indexs = smallest.indexOf(smallest);" gives me an error. How do you fix it?
• November 28th, 2013, 10:06 PM
dineshj83
Re: How to find the array index of the lowest value in the array?
There is no indexOf method of primitive int which is the dataType of smallest and hence you are getting the error. You need to loop through the entire array and find out the value which is lowest and its corresponding index.
• November 29th, 2013, 05:57 AM
Norm
Re: How to find the array index of the lowest value in the array?
Quote:

gives me an error.
Please copy the full text of the error message and paste it here. It has important info about the error.