I have to write a program that finds the smallest index of an array and am having a bit of trouble. Can anyone find anything wrong with this?

import java.util.Scanner;

public class SmallestIndex {
	
	static final int LINESIZE = 10;
	static final int MAXSIZE = 50;
	static final int MINVALUE = Integer.MIN_VALUE;

	//methods

	//This method returns the index of the smallest value in the array of given size
	public static int smallestIndex (int[] array, int size) {
		//declare local variables
		int currentValue = array[0]; //assumes that size > 0
		int smallestIndex = 0;
		for (int j=1; j < size; j++) {
			if (array[j] < currentValue); //compare currentVlaue with array[j] and if array[j] is smaller,
			currentValue = array[j];//update currentValue and smallestIndex with new smaller values
		}
		return smallestIndex;
	
	}
	

	public static void printArray(int[] array, int size) {
		//This method prints the array to the screen
		System.out.println("The numbers in the array are:");
		for (int j=0; j<size; 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;
				}
			}
			return data;
		}
	public static void main(String[] args) {
		// declare variables
		int index; // stores the value of smallest index
		int [] array = null;
		int [] numbers = null;
		int size = 0;
	
		//print the array 
		for (int j=0; j < size; j++)
			System.out.print(numbers[j] + " ");


		//compute smallest index by calling the method you created above
		index = smallestIndex(array, size);
		System.out.println("The smallest index is " + index);
		
	
	}

}
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;
    //methods
    //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]; //assumes that size > 0
        int smallestIndex = 0;
        for (int j=1; j < arrSize; j++) {
            if (array[j] < currentValue) //compare currentVlaue with array[j] and if array[j] is smaller,
            currentValue = array[j];//update currentValue and smallestIndex with new smaller values
        }
        System.out.println();
        System.out.println("Smallest number is "+currentValue);
    }
    public static void printArray(int[] array) {
        //This method prints the array to the screen
        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 void main(String[] args) {
        // declare variables
        int index; // stores the value of smallest index
        int [] array = getArrayOfNumbers();
        printArray(array);
        int [] numbers = null;
        smallestIndex(array);
    }
}

Edited 3 Years Ago by Reverend Jim: Fixed formatting

Thank you so much! I looked over the code you posted a realized what i did wrong. Again, thank you for your help

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