-3

I'm creating a program of letting people enter 10 integers and display them from the smallest to the largest. Here is my program:

import java.util.Scanner;

public class EnterTenNumbers { public static void main(String[] args){

    System.out.println("Enter 10 numbers");
    int small=0;
    for(int i=0; i<10; i++){
        Scanner in=new Scanner(System.in);
        int[] i1=new int[10];
        int num=in.nextInt();
        i1[i]=num;
        if(i1[i]<i1[i+1] || i1[i]==i1[i+1]){
            System.out.println(i1);
        }else if(i1[i]>i1[i+1]){
            i1[i+1]=i1[i];
            System.out.println(i1);
        }
    }
}

}

When I run my program, after the user input the numbers, such things like "[I@55f96302" appear.

And after the user entered 10 integers, Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 10 at EnterTenNumbers.main(EnterTenNumbers.java:13) appears while it should display the numbers from smallest to largest.

What happened?

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
Yang
  • 11
  • 1
  • There are numerous problems with the code snippet you posted unfortunately. Start by reading these posts: http://stackoverflow.com/questions/4455873/java-arraylist-to-store-user-input and http://stackoverflow.com/questions/16252269/how-to-sort-a-arraylist-in-java and have another crack at it! :) – muzzlator Mar 24 '17 at 03:05
  • Wrong hints. He probably has to use arrays, and his problem is that he doesn't know what he is doing. Your links do not help with that. – GhostCat Mar 24 '17 at 03:22
  • And for the problem itself: read what the exception message is telling you. The solution is in front of your eyes. – GhostCat Mar 24 '17 at 03:24

2 Answers2

1

When I run my program, after the user input the numbers, such things like "[I@55f96302" appear.

This is because you are printing the string representation of your array, not the elements of the array.

Replace

System.out.println(i1);

with

System.out.println(i1[i]);

And after the user entered 10 integers, Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 10

As the exception clearly indicates, you are trying to access 10th index (index starts from 0) or 11th element of the array. But your array has only 10 elements. So that's why you get AIOOBE. In your conditional statement, the predicate i1[i+1] becomes i1[10] when i=9. You need to fix this.

Lastly, you can achieve your objective the following way too:

System.out.println("Enter 10 numbers");
int small=0;
for(int i=0; i<10; i++){
    Scanner in=new Scanner(System.in);
    int[] i1=new int[10];
    int num=in.nextInt();
    i1[i]=num;
}
Arrays.stream(i1).sorted().forEach(System.out::println);
VHS
  • 9,534
  • 3
  • 19
  • 43
  • Thank you, but the problem is that in this way, the variable "i1" will be declared outside the loop, which will produce another error. Should I declare the array twice? – Yang Mar 25 '17 at 13:10
  • @Yang, absolutely right. You need to declare it outside the `for` loop. After you make these revisions and if you still face any errors, please post the revised code and let us know again. – VHS Mar 25 '17 at 18:46
1
  1. Every time you are asking the user to give a number as input, you are creating a new array of integers. Aren't you supposed to create only one array to hold those 10 integers ? So, declare the integer array outside the loop. in this way, it will be declared only once.

  2. You have to take all the 10 integers from user at the very beginning, then you have to run the operations over these 10 integers in order to show them from smallest to the largest.

  3. after taking all the 10 integers from user and saving them in An array, you have to again loop through among those numbers one by one and check which number is smallest and which number is largest. When the loop check the last index of the array, you can't check the next index of that array (because that index of the array doesn't exists). That's why you are having an exception every time.

    if(i1[i]<i1[i+1] || i1[i]==i1[i+1]){
        System.out.println(i1);
    }else if(i1[i]>i1[i+1]){
        i1[i+1]=i1[i];
        System.out.println(i1);
    }