# ArrayIndexOutOfBoundsException...Help!!

• February 17th, 2013, 06:07 PM
112297
ArrayIndexOutOfBoundsException...Help!!
I'm writing an array program that has the user input a series of integers, and the program has to count how many duplicates of a number are entered if any and output the data in increasing order..I almost have it but I am stuck trying to figure out why I am getting an out of bounds error for the loop in the numSame function...What am I doing wrong? When It doesn't give the out of bounds error, the counter isn't tallying how many duplicates there are...It always outputs zero..my sample data is 12,34,56,78,12,34,56,78

Code :

```  import java.util.*;   public class arrayProgram{ static Scanner console = new Scanner(System.in);     public static void main(String[] args) {   int[] numList = new int[100]; int[] listCopy = new int[100]; int[] numCount = new int[100];     readData(numList); copyList(numList,listCopy); numSame(numList,listCopy,numCount); Arrays.sort(numList); for(int num=0;num<numList.length;num++) { System.out.println("Look---> " + numList[num]);   } }     public static void readData(int[] list) { int i;   System.out.print("Enter a series of positive integers: ");   for (i = 0; i < list.length; i++){ list[i] = console.nextInt(); if (list[i] == -999) break;}   System.out.println(); }     public static void copyList(int list[], int list2[]) { int index;   for (index = 0; index < list.length; index++) { list2[index]=list[index]; System.out.println(list2[index]);}   }   public static void numSame(int[] list1, int[] list2, int[] list3) { int counter = 0; int k = 0; for(int i : list1) { for(int j : list2) { if(list1[i] == list2.length) { counter++; list3[k] = counter; k++;   } }     } System.out.println("Counter : " + counter);   } }```
• February 17th, 2013, 06:51 PM
Norm
Re: ArrayIndexOutOfBoundsException...Help!!
Quote:

I am getting an out of bounds error
When you get an error, please copy the full text of the error message and paste it here. It has valuable info about the problem.
• February 17th, 2013, 07:06 PM
112297
Re: ArrayIndexOutOfBoundsException...Help!!
Quote:

Originally Posted by Norm
When you get an error, please copy the full text of the error message and paste it here. It has valuable info about the problem.

Sorry,
Here is the error:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: -999
at arrayProgram.numSame(arrayProgram.java:70)
at arrayProgram.main(arrayProgram.java:21)
• February 17th, 2013, 07:20 PM
Norm
Re: ArrayIndexOutOfBoundsException...Help!!
Quote:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: -999
at arrayProgram.numSame(arrayProgram.java:70)
at arrayProgram.main(arrayProgram.java:21)
That is a very strange index value: -999
Do you recognize where that came from? Why is the code trying to use that for an index?

Look at line 70 and see why the code was using -999 for an index.

Can you explain in detail what the logic of the program is? What are all the large arrays for? Is their size related to the data that is entered?
• February 17th, 2013, 07:34 PM
112297
Re: ArrayIndexOutOfBoundsException...Help!!
The -999 is supposed to be a sentinel controlled loop...It's supposed to end the input from the user, but I don't know why it's being used in the program like it is...

Quote:

Originally Posted by Norm
That is a very strange index value: -999
Do you recognize where that came from? Why is the code trying to use that for an index?

Look at line 70 and see why the code was using -999 for an index.

Can you explain in detail what the logic of the program is? What are all the large arrays for? Is their size related to the data that is entered?

--- Update ---

I'm supposed to assume that the data set has at most 100 numbers, and -999 marks the end of the input data...
• February 17th, 2013, 07:43 PM
Norm
Re: ArrayIndexOutOfBoundsException...Help!!
Why does the code use the sentinel number for an index? Is there a way the code can test for that number and use that number to stop processing the input data instead to using it as data?

Can you explain in detail what the logic of the program is? What are all the large arrays for? Is their size related to the data that is entered?
• February 17th, 2013, 08:04 PM
112297
Re: ArrayIndexOutOfBoundsException...Help!!
I can't understand why the -999 is being counted as part of that data...it breaks out of the loop just fine..

Here is the problem from the book: Write a program that reads in a set of positive integers, and outputs how many times a particular set of integers appears in the list. You may assume that the data set has at most 100 numbers, and -999 marks the end of the input data. The numbers must be output in increasing order.

Quote:

Originally Posted by Norm
Why does the code use the sentinel number for an index? Is there a way the code can test for that number and use that number to stop processing the input data instead to using it as data?

Can you explain in detail what the logic of the program is? What are all the large arrays for? Is their size related to the data that is entered?

• February 17th, 2013, 09:51 PM
Norm
Re: ArrayIndexOutOfBoundsException...Help!!
Quote:

I can't understand why the -999 is being counted as part of that data
Use this code to print out the array so you can see what is in it:
Code :

`System.out.println("an ID "+ java.util.Arrays.toString(theArrayNameHere));`
Use the name of your array.
Quote:

Here is the problem from the book: ....
What you posted were the requirements, not a description of the program's logic.
Given the requirements for the program, now you need to do some design work and decide what steps the program needs to take to do it.
Before working on the code any more, make a list of the steps the code needs to take and what variables will be needed to work with.
• February 17th, 2013, 11:09 PM
112297
Re: ArrayIndexOutOfBoundsException...Help!!
I have 3 arrays...The numList array accepts input values from the console. The 2nd array listCopy has the values from the numList array deep copied into it...I did that so I could use it to compare with the numList array to see how many duplicate numbers are entered...The 3rd array numSame, is supposed to hold the values of the number of duplicates...I use Arrays.sort to print out the numbers in increasing order...I understand everything that's going on except the error I'm getting...It keeps saying out of bounds on the line where the variable 'counter' is attempting to assign it's value to list3[k] ...it's somethings about the third array...I'm stumped..

Quote:

Originally Posted by Norm
Use this code to print out the array so you can see what is in it:
Code :

`System.out.println("an ID "+ java.util.Arrays.toString(theArrayNameHere));`
Use the name of your array.

What you posted were the requirements, not a description of the program's logic.
Given the requirements for the program, now you need to do some design work and decide what steps the program needs to take to do it.
Before working on the code any more, make a list of the steps the code needs to take and what variables will be needed to work with.

• February 18th, 2013, 06:19 AM
Norm
Re: ArrayIndexOutOfBoundsException...Help!!
Quote:

so I could use it to compare with the numList array to see how many duplicate numbers are entered..
I don't understand what the copy is needed for. Why not use the array that has the data entered by the user?
Quote:

3rd array numSame, is supposed to hold the values of the number of duplicates
Please explain how the array can hold the number of duplicates. How does the code detect a duplicate?
How does the code count duplicates?
What if these were the numbers entered by the user: 1, 1000, 123456, 2345678, 123456, 1000
how would the code work with those numbers?

Why doesn't the code test for the -999 value that was saved in the array to detect the end of the data?

Quote:

use Arrays.sort to print out the numbers in increasing order.
Why do you sort the numbers in the array after the search for duplicates?
• February 18th, 2013, 02:31 PM
SunshineInABag
Re: ArrayIndexOutOfBoundsException...Help!!
Quote:

Originally Posted by 112297
I have 3 arrays...The numList array accepts input values from the console. The 2nd array listCopy has the values from the numList array deep copied into it...I did that so I could use it to compare with the numList array to see how many duplicate numbers are entered...The 3rd array numSame, is supposed to hold the values of the number of duplicates...I use Arrays.sort to print out the numbers in increasing order...I understand everything that's going on except the error I'm getting...It keeps saying out of bounds on the line where the variable 'counter' is attempting to assign it's value to list3[k] ...it's somethings about the third array...I'm stumped..

The program outputs zero because the numSame method is calling the 3 arrays you declared at the start. In other words, they're empty, so your if-statement is evaluating whether 0 equals the array length (100).

Why not just search for duplicates directly from the numList array? Instead of checking for duplicates, the numSame method increments a counter if values from 1 array equal the length of another array, where both arrays have the same length. The third array stores values from the counter, which I'm not understanding the reason for. Your program isn't complete and I suggest you lay out the logic, then start over.