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

By definition the smallest index in an array in Java is always 0

i guess i should rephrase the way i stated it. I need to find the index of the smallest number in the array. so if the array is inputed like this:

3 43 0 9 2 -1

-1 is the smallest number and the index of -1 is 6, so I need to output 6.

I am having trouble figuring out exactly how to find that index

You're very close :P

//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];
                                smallestIndex = j;
                        }
		}
		System.out.println();
		System.out.println("The smallest index is: "+ smallestIndex);
	}

With the modified code, every time a number is found that is less than the one at the "smallest index," both the smallest number AND the smallest index found need to be updated.

Assign smallestindex to j and print smallestindex instead of currentValue
is this code written by you, reason of this question is that if someone is able to write this much of code then how come he is missing one simple line...

Assign smallestindex to j and print smallestindex instead of currentValue
is this code written by you, reason of this question is that if someone is able to write this much of code then how come he is missing one simple line...

yes this is written by me. I have been working on it for awhile and the way to find the index slipped my mind and i couldnt remember it for the life of me

Edited 6 Years Ago by javarook: n/a

vchandra, don't act skeptical or condescending toward ppl just because they have stupid errors. I am fairly good at coding and I still make stupid errors all the time. It's kind of like editing an essay -- it's easier to edit if you aren't the writer. I think the only way to get good at preventing errors is by debugging other peoples codes -- if you can find other people's errors then you will be better at finding your own.

This article has been dead for over six months. Start a new discussion instead.