Results 1 to 9 of 9
  1. #1
    krissy992 is offline Member
    Join Date
    Jan 2013
    Posts
    9
    Rep Power
    0

    Default Exception in thread "main"

    I have a problem with my code. I'm trying to make a program that will ask the user for two sets of sorted data and then combine those two sets into a numerically organized new array.

    I keep getting the error: Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 9 at MergeAndSort.main(MergeAndSort.java:45)

    Please help!

    Java Code:
    
    
    import java.util.Scanner;
    public class MergeAndSort 
    {
    
    	
    	public static void main(String[] args) 
    	{
    		Scanner input = new Scanner(System.in);
    		//Ask user for two lists of numbers already sorted.
    		
    		System.out.print("Enter a sorted list of values: ");				       //Obtain the first list from user
    		int i = input.nextInt();
    		int list1[] = new int[i+1];
    		
    		System.out.print("Enter the second sorted list of values: ");		//Obtain second list from user
    		int j = input.nextInt();
    		int list2[] = new int[j+1];
    		
    		int listMerged [] = new int[list1.length + list2.length];			//Create new array with length of both lists combined
    		
    		for(int d = 0; d < listMerged.length; d++)								//Starting with the first list1[0], check all numbers
    		{
    			if (i < list1.length && j < list2.length)
    			{																//If list1's length and list2's length have values left, continue
    				if(list1[i] < list2[j])	
    				{															//If list1's item is less than list2's item,
    					listMerged[d] = list1[i];								//add it to the listMerged array.
    					d++;
    				}
    				else if (list1[i] >= list2[j])								//If list2's item is less than list1's item,
    				{															//add it to the merged list
    					listMerged[d] = list2[j];
    					j++;
    				}
    			}
    			else if (i < list2.length)										//If the list1 array is empty, but list2 is not
    			{																//add these values to the merged list
    				listMerged[d] = list2[j];
    				j++;
    			}
    			else															//If the list2 array is empty but list1 is not
    			{	
    				listMerged[d] = list1[i];
    				i++;														//add these values to the merged list
    			}
    		}
    		
    		System.out.println(listMerged[i]);
    	
    
    	}
    
    }

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

  3. #3
    krissy992 is offline Member
    Join Date
    Jan 2013
    Posts
    9
    Rep Power
    0

    Default Re: Exception in thread "main"

    It is where it says listMerged[d] = list2[j];

  4. #4
    wsaryada is offline Senior Member
    Join Date
    Jun 2007
    Location
    Bali, Indonesia
    Posts
    760
    Rep Power
    8

    Default Re: Exception in thread "main"

    I found two of that kind statements in your program. And my assumption is the statement is in the line 38. And this is also just my assumption, starting from line 36 you have something like:

    Java Code:
    else if (i < list2.length)                                       //If the list1 array is empty, but list2 is not
    {                                                               //add these values to the merged list
        listMerged[d] = list2[j];
        j++;
    }
    I think what you should test in there is the value of "j" instead of "i". So the line should be if (j < list2.length).

  5. #5
    krissy992 is offline Member
    Join Date
    Jan 2013
    Posts
    9
    Rep Power
    0

    Default Re: Exception in thread "main"

    Hmm, when I do that I get the same error but for line 51 which is listMerged[d] = list1[i];

    It seems to be something with the i variable.

  6. #6
    wsaryada is offline Senior Member
    Join Date
    Jun 2007
    Location
    Bali, Indonesia
    Posts
    760
    Rep Power
    8

    Default Re: Exception in thread "main"

    You can debug your program if you are using an IDE. Watch what is the value of the "d", "i", and "j" when you try to access / manipulate the array. The error basically means that you are trying to access the array out side of their boundary. If you don't use IDE just print out the value of those variables.

  7. #7
    krissy992 is offline Member
    Join Date
    Jan 2013
    Posts
    9
    Rep Power
    0

    Default Re: Exception in thread "main"

    WHen I debug I don't get any values. Also, when I try to print any values, I first have to type the values I want in and then I get that error again when the text "Enter the second sorted list of values: " appears

  8. #8
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,648
    Blog Entries
    7
    Rep Power
    21

    Default Re: Exception in thread "main"

    Shouldn't you reset both i and j to zero before that loop starts? Also, line #28 is incorrect (increment i instead).

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  9. #9
    krissy992 is offline Member
    Join Date
    Jan 2013
    Posts
    9
    Rep Power
    0

    Default Re: Exception in thread "main"

    Hmm, I changed those values because that would make sense. Haha. But now when the second string prints to ask the user for their second set of numbers, my console just displays a 0 afterwards, no way to type. This is the first time I've messed with arrays really.

Similar Threads

  1. Replies: 1
    Last Post: 11-02-2012, 11:38 PM
  2. Replies: 2
    Last Post: 06-20-2012, 12:18 AM
  3. Replies: 5
    Last Post: 12-21-2011, 06:44 PM
  4. Replies: 14
    Last Post: 04-08-2011, 06:01 PM
  5. Replies: 5
    Last Post: 06-22-2010, 03:51 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •