yves
yves

Reputation: 15

Can't seem to fix my 'java.lang.ArrayIndexOutOfBoundsException' error

I'm unsure if the problem in my program is simply in where I declared the arrays, or if I'm stuffing one of them with more than they can take.

Here is my code:

import java.util.Scanner;
import java.util.Arrays;

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

    int[] numberList = new int[10];

    System.out.println("Please enter ten integers.");

    Scanner input = new Scanner(System.in);

    for (int i = 0; i < numberList.length; i++) {

        numberList[i] = input.nextInt();

        }

    int[] newNumberList = new int[10];

    newNumberList = eliminateDuplicates(numberList);


    System.out.println("The discrete numbers are: ");

    for (int i = 0; i < newNumberList.length; i++) {

            System.out.println(newNumberList[i] + ' ');

        }

    }

public static int[] eliminateDuplicates(int[] numberList) {

    int[] noDuplicateList = new int[numberList.length];

    int size = 0;

    java.util.Arrays.sort(numberList);

    for (int i = 0; i < numberList.length; i++) {

        if (numberList[i] != numberList[i+1]) {

            noDuplicateList[i] = numberList[i];

            size++;
        }

    }

    return noDuplicateList;
}

}

This is the output + the error message I get:

Please enter ten integers.
9
8
7
3
3
4
1
2
8
6
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 10
at EliminateDuplicates.eliminateDuplicates(EliminateDuplicates.java:51)
at EliminateDuplicates.main(EliminateDuplicates.java:28)

Upvotes: 0

Views: 90

Answers (4)

Eran
Eran

Reputation: 393791

Your problem is here :

 for (int i = 0; i < numberList.length - 1; i++) {
     if (numberList[i] != numberList[i+1]) {

When i reaches numberList.length-1, i+1 is out of the array bounds.

You should change the range of the loop. You shold also use the size index when assigning to the noDuplicateList list, otherwise you will have gaps in that list.

for (int i = 0; i < numberList.length - 1; i++) {
    if (numberList[i] != numberList[i+1]) {
        noDuplicateList[size] = numberList[i];
        size++;
    }
}

You would still have a minor problem. Since your eliminateDuplicates methods returns an array of the same size as the input, if there were duplicates, the output array would have unused indices in its end (that will contain zeroes). If you wish to avoid that, you can add all the items of the input array to a HashSet, find the size() of that set, and create an array of that size for the output. Using a Set will also simplify your code, since the Set would eliminate the duplicates for you, and you don't have to sort the input array.

public static int[] eliminateDuplicates(int[] numberList) {

    Set<Integer> noDups = new HashSet<Integer>();

    for (int num : numberList)  
        noDups.add(num);

    int[] noDuplicateList = new int[noDups.size()];

    Iterator<Integer> iter = noDups.iterator();
    for (int i=0; i<noDuplicateList.length && iter.hasNext();i++)
         noDuplicateList[i]=iter.next();

    return noDuplicateList;
}

Upvotes: 3

Jens
Jens

Reputation: 69440

the problem is this line:

if (numberList[i] != numberList[i+1]) {

If i is equals numberList.length the second condition is out of bounds.

So you have to change:

for (int i = 0; i < numberList.length; i++) {

TO:

for (int i = 0; i < numberList.length-1; i++) {

Upvotes: 0

Xline
Xline

Reputation: 812

if (numberList[i] != numberList[i+1])

when i=9 this would look for index 10 which is out of bound.

Upvotes: 0

user2575725
user2575725

Reputation:

Your problem is here, eliminateDuplicates:

if (numberList[i] != numberList[i+1]) {// i+1, for the last elements raise the exception

Upvotes: 0

Related Questions