Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java - Finding Largest and Smallest Numbers using an Array

Tags:

java

arrays

I am supposed to make a program that takes 10 numbers from a user input, find the largest and smallest number and display all the inputs from the user. This program does use an array. Here is my code:

import java.util.Scanner; // program uses Scanner
public class ArrayTester {
    // begin execution
    public static void main(String[] args) {
        // declare and create array object
        // declare smallest and largest int variables 
        int[] numbers;
        numbers = new int[10];
        int smallest = numbers[0], largest = numbers[0];

        // create Scanner object
        Scanner input = new Scanner(System.in);

        // prompt user 
        System.out.print("Please enter 10 numbers: \n");
        // use for loop to obtain user input
        for (int counter = 0; counter < numbers.length; counter++) {
            numbers[counter] = input.nextInt();
        } // end obtaining input

        // enhanced for loop to find largest and smallest values
        for (int i : numbers) {
            if (numbers[i] < smallest) {
                smallest = numbers[i];
            } // end finding smallest
            else if (numbers[i] > largest) {
                largest = numbers[i];
            } // end finding largest number 
        } // end finding largest and smallest values

        // for loop to print user input 
        System.out.printf("%s%8s\n", "Index", "Input");
        for (int counter = 0; counter <= numbers.length; counter++) {
            System.out.printf("%5d%8d\n", counter, numbers[counter]);
        } // end printing input values

        // print smallest and largest numbers
        System.out.printf("Smallest number: %d\nLargest number: %d\n", smallest, largest);
        System.out.print("Programmed by Christian Lapinig");
    } // end main
 } // end ArrayTester

At this point I am able to obtain user inputs, but I run into this problem:

Please enter 10 numbers: 
454
392
33
41
6
44
39
21
12
2
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 454
at ArrayTester.main(ArrayTester.java:32)

Would I need a try and catch block to fix this?

like image 519
Christian Lapinig Avatar asked Dec 21 '25 23:12

Christian Lapinig


2 Answers

Your problem is related to the way you use the for-loop as already stated in the other answers. A shorter approach in Java 8 though, would be using a stream:

IntStream.of(numbers).max() and IntStream.of(numbers).min()

like image 161
Bubletan Avatar answered Dec 23 '25 13:12

Bubletan


A try-catch would just swallow the exception. Your problem is that the enhanced for loop is iterating over the values of the array, but you're treating it like it's the index, so you check numbers[454] and immediately blow up because you're outside the length of the array.

Either iterate over the indexes, or just work with the values directly:

for (int i : numbers) {
    if (i < smallest) {
        smallest = i;
    } // end finding smallest
    else if (i > largest) {
        largest = i;
    } // end finding largest number 
} // end finding largest and smallest values
like image 43
Danny Avatar answered Dec 23 '25 13:12

Danny