Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 11 of 11

Thread: ArrayIndexOutOfBoundsException...Help!!

  1. #1
    Junior Member
    Join Date
    Nov 2012
    Posts
    9
    Thanks
    1
    Thanked 1 Time in 1 Post

    Default 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


     
    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);
     
             }
          }

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    18,719
    Thanks
    45
    Thanked 1,974 Times in 1,948 Posts

    Default Re: ArrayIndexOutOfBoundsException...Help!!

    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.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Nov 2012
    Posts
    9
    Thanks
    1
    Thanked 1 Time in 1 Post

    Default Re: ArrayIndexOutOfBoundsException...Help!!

    Quote Originally Posted by Norm View Post
    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)

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    18,719
    Thanks
    45
    Thanked 1,974 Times in 1,948 Posts

    Default Re: ArrayIndexOutOfBoundsException...Help!!

    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?
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Nov 2012
    Posts
    9
    Thanks
    1
    Thanked 1 Time in 1 Post

    Default 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 View Post
    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...

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    18,719
    Thanks
    45
    Thanked 1,974 Times in 1,948 Posts

    Default 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?
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    Nov 2012
    Posts
    9
    Thanks
    1
    Thanked 1 Time in 1 Post

    Default 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 View Post
    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?
    Last edited by 112297; February 17th, 2013 at 08:07 PM. Reason: Correction about loop

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    18,719
    Thanks
    45
    Thanked 1,974 Times in 1,948 Posts

    Default Re: ArrayIndexOutOfBoundsException...Help!!

    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:
    System.out.println("an ID "+ java.util.Arrays.toString(theArrayNameHere));
    Use the name of your array.
    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.
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Junior Member
    Join Date
    Nov 2012
    Posts
    9
    Thanks
    1
    Thanked 1 Time in 1 Post

    Default 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 View Post
    Use this code to print out the array so you can see what is in it:
    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.

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    18,719
    Thanks
    45
    Thanked 1,974 Times in 1,948 Posts

    Default Re: ArrayIndexOutOfBoundsException...Help!!

    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?
    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?

    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?
    If you don't understand my answer, don't ignore it, ask a question.

  11. #11
    Member
    Join Date
    Feb 2013
    Location
    Canada
    Posts
    54
    Thanks
    0
    Thanked 6 Times in 6 Posts

    Default Re: ArrayIndexOutOfBoundsException...Help!!

    Quote Originally Posted by 112297 View Post
    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.