Reputation: 43
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?
Upvotes: 3
Views: 21671
Reputation: 33
As many people pointed out, you are using the values of your for-each loop as and index, meaning your code tries to access the position of whatever number your user inputs in your array.
So to fix it, you just need to do
smallest = i;
and
largest = i;
Upvotes: 0
Reputation: 6306
In java 8, you can obtain an IntStream
and get the summary statistics.
final IntSummaryStatistics stats = IntStream.of(numbers).summaryStatistics();
Upvotes: 0
Reputation: 3863
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()
Upvotes: 2
Reputation: 900
Change it to :
for (int i=0 ;i< numbers.length; i++) {
if (numbers[i] < smallest) {
smallest = numbers[i];
} // end finding smallest
else if (numbers[i] > largest) {
largest = numbers[i];
} // end finding largest number
}
AND <= to <
for (int counter = 0; counter < numbers.length; counter++) {
System.out.printf("%5d%8d\n", counter, numbers[counter]);
}
Try catch block will simply handle your error in a graceful way . It won't solve your problem.
Upvotes: 0
Reputation: 115328
Your problem is in this loop:
for (int i : numbers) {
This loop means iterates over the elements of the array, not its indexes. So, you can say:
for (int a : numbers) {
if(a > ...) {
....
}
Alternatively you can use array index like the following"
for (int i = 0; i < numbers.lenth; i++) {
if(numbers[i] > ...) {
.....
}
Upvotes: 0
Reputation: 7398
CHange
for (int i : numbers) {
to
for (int i=0;i<numbers.length;i++) {
Upvotes: 0
Reputation: 1760
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
Upvotes: 1
Reputation: 3449
for (int counter = 0; counter <= numbers.length; counter++)
You put =<
, instead you should put <
. if counter equals to length, it tries to reach out of range because indexes start from zero.
Upvotes: 1