0

I need to find the smallest index in an array, my math must be wrong because i am finding the smallest number not the smallest index. can someone check my smallestIndex method and give me some help?

for example: if i put in these four numbers, 2 3 0 1, the output will be 0, but it really should be 2 because that is what the index of 0 actually is.

here is the snippet of just the smallestIndex method:

```
public static void smallestIndex (int[] array) {
int currentValue = array[0];
int smallestIndex = 0;
for (int j=1; j < arrSize; j++) {
if (array[j] < currentValue)
currentValue = array[j];
}
System.out.println();
System.out.println("The smallest index is: "+ currentValue);
}
```

here is the whole code:

```
import java.util.Scanner;
public class SmallestIndex {
static final int LINESIZE = 10;
static final int MAXSIZE = 50;
static final int MINVALUE = Integer.MIN_VALUE;
private static int arrSize = 0;
//This method returns the index of the smallest value in the array of given size
public static void smallestIndex (int[] array) {
int currentValue = array[0];
int smallestIndex = 0;
for (int j=1; j < arrSize; j++) {
if (array[j] < currentValue)
currentValue = array[j];
}
System.out.println();
System.out.println("The smallest index is: "+ currentValue);
}
public static void printArray(int[] array) {
System.out.println("The numbers in the array are: ");
for (int j=0; j< arrSize; j++) {
if (j%LINESIZE == 0 )
System.out.println();
System.out.printf("%5d", array[j]);
}
}
//prompt user for size of the array and then that many values to fill the array
public static int[] getArrayOfNumbers() {
int[] data = new int[MAXSIZE];
for (int j=0; j<data.length; j++) {
data[j] = MINVALUE;
}
int index = 0;
// prompt user for data and store it
System.out.println("Please enter up to "+MAXSIZE+" integers separated by spaces.\n" +
"End the input with any letter" );
Scanner input = new Scanner(System.in);
while (input.hasNextInt()) {
try {
data[index] = input.nextInt();
index++;
}
catch (Exception e) {
System.out.println("Wrong input, so quitting");
return null;
}
}
arrSize = index;
return data;
}
public static int getArraySize(int[] array) {
int size = 0;
try {
while (array[size] != MINVALUE)
size++;
}
catch (ArrayIndexOutOfBoundsException e) {
return size;
}
return size;
}
public static void main(String[] args) {
// declare variables
int index;
int [] array = getArrayOfNumbers();
printArray(array);
int [] numbers = null;
smallestIndex(array);
}
}
```

*Edited 6 Years Ago by javarook*: n/a