Results 1 to 9 of 9
Thread: Exception in thread "main"
- 01-31-2013, 04:58 AM #1
Member
- Join Date
- Jan 2013
- Posts
- 9
- Rep Power
- 0
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]); } }
-
Re: Exception in thread "main"
Which line is line 45?
- 01-31-2013, 05:02 AM #3
Member
- Join Date
- Jan 2013
- Posts
- 9
- Rep Power
- 0
Re: Exception in thread "main"
It is where it says listMerged[d] = list2[j];
- 01-31-2013, 05:19 AM #4
Senior Member
- Join Date
- Jun 2007
- Location
- Bali, Indonesia
- Posts
- 762
- Rep Power
- 13
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++; }
Website: Learn Java by Examples
- 01-31-2013, 05:23 AM #5
Member
- Join Date
- Jan 2013
- Posts
- 9
- Rep Power
- 0
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.
- 01-31-2013, 05:34 AM #6
Senior Member
- Join Date
- Jun 2007
- Location
- Bali, Indonesia
- Posts
- 762
- Rep Power
- 13
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.
Website: Learn Java by Examples
- 01-31-2013, 05:43 AM #7
Member
- Join Date
- Jan 2013
- Posts
- 9
- Rep Power
- 0
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
- 01-31-2013, 10:19 AM #8
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 14,422
- Blog Entries
- 7
- Rep Power
- 28
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,
JosBuild a wall around Donald Trump; I'll pay for it.
- 01-31-2013, 05:40 PM #9
Member
- Join Date
- Jan 2013
- Posts
- 9
- Rep Power
- 0
Similar Threads
-
Error: "Exception in thread "main" java.util.NoSuchElementException"
By Mattiscool in forum New To JavaReplies: 1Last Post: 11-03-2012, 12:38 AM -
"Exception in thread "main" java.lang.NoSuchMethodError: main"
By isnkumar in forum New To JavaReplies: 2Last Post: 06-20-2012, 01:18 AM -
Got struck with this :- " Exception in thread "main" java.lang.NullPointerException"
By Vermont in forum New To JavaReplies: 5Last Post: 12-21-2011, 07:44 PM -
Exception in thread "main" java.lang.NumberFormatException:input string: "060320
By renu in forum New To JavaReplies: 14Last Post: 04-08-2011, 07:01 PM -
Question about error "Exception in thread "main" java.lang.NoSuchMethodError: main
By ferdzz in forum New To JavaReplies: 5Last Post: 06-22-2010, 04:51 PM
Bookmarks