Results 1 to 3 of 3
Thread: problem with insertion sort???
- 03-22-2010, 08:54 AM #1
Member
- Join Date
- Nov 2009
- Location
- California
- Posts
- 55
- Rep Power
- 0
problem with insertion sort???
Basically when I use my client program the output looks like this:Java Code://-------------------------------------------------------------------------------------------------------------------------------- // generate method generates four random numbers between one and six, then selects the three highest values and returns their sum //-------------------------------------------------------------------------------------------------------------------------------- public int generate(boolean trueOrFalse){ int sum = 0; if (trueOrFalse){ System.out.println("Generating Character's level..."); num1 = gen.nextInt(5)+1; System.out.println("Roll One: " + num1); num2 = gen.nextInt(5)+1; System.out.println("Roll Two: " + num2); num3 = gen.nextInt(5)+1; System.out.println("Roll Three: " + num3); num4 = gen.nextInt(5)+1; System.out.println("Roll Four: " + num4); int[] fourRollVals = {num1, num2, num3, num4}; System.out.println("Sorting the four rolls into the order of lowest to highest..."); //Sort the values of the 4 rolls for (int index = 1; index < 4; index++) // index refers to the element that is "trying to find its place" { int key = fourRollVals[index]; int position = index; // shift larger values to the right while (position > 0 && fourRollVals[position-1] >= key) { fourRollVals[position] = fourRollVals[position-1]; position--; } fourRollVals[position] = key; } int length = fourRollVals.length; int temp; for(int counter=0; counter<length-1; counter++) { //Loop once for each element in the array. for(int index=0; index<length-1-counter; index++) { //Once for each element, minus the counter. if(fourRollVals[index] > fourRollVals[index+1]) { //Test if need a swap or not. temp = fourRollVals[index]; //These three lines just swap the two elements: fourRollVals[index] = fourRollVals[index+1]; fourRollVals[index+1] = temp; } } } System.out.println("The Sorted Array of the Rolls is: " + fourRollVals); sum = (fourRollVals[2] + fourRollVals[3] + fourRollVals[4]); System.out.println("Therefore, the Character's level is: " + sum); return sum; } else{ num1 = gen.nextInt(6)+1; num2 = gen.nextInt(6)+1; num3 = gen.nextInt(6)+1; num4 = gen.nextInt(6)+1; int[] fourRollVals = {num1, num2, num3, num4}; //Sort the values of the 4 rolls for (int index = 1; index < 4; index++) // index refers to the element that is "trying to find its place" { int key = fourRollVals[index]; int position = index; // shift larger values to the right while (position > 0 && fourRollVals[position-1] > key) { fourRollVals[position] = fourRollVals[position-1]; position--; } fourRollVals[position] = key; } sum = (fourRollVals[2] + fourRollVals[3] + fourRollVals[4]); return sum; } } //xxxENDxMETHODxxxEndxMETHODxxxENDxMETHODxxxEndxMETHODxxxENDxMETHODxxxEndxMETHODxxxENDxMETHODxxxEndxMETHODxxxENDxMETHODxxxEndxMETHODxxxxx //xxxENDxMETHODxxxEndxMETHODxxxENDxMETHODxxxEndxMETHODxxxENDxMETHODxxxEndxMETHODxxxENDxMETHODxxxEndxMETHODxxxENDxMETHODxxxEndxMETHODxxxxx
Generating Character's level...
Roll One: 2
Roll Two: 4
Roll Three: 5
Roll Four: 1
Sorting the four rolls into the order of lowest to highest...
The Sorted Array of the Rolls is: [I@a90653
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 4
at ScoreGenerator.generate(ScoreGenerator.java:95)
at Adventurer.<init>(Adventurer.java:28)
at Wizard.<init>(Wizard.java:41)
at AdventurerDemo.main(AdventurerDemo.java:11)
I don't have an issue w/ the error above ^^^^
but my problem is that why is my sort method not working???
Clearly: [I@a90653 is wrong
how do i fix this?
- 03-22-2010, 10:27 AM #2
Member
- Join Date
- Nov 2009
- Location
- California
- Posts
- 55
- Rep Power
- 0
anyone who reads this please ignore this post.
I didn't know that cross posting is against the rules and I shall never do it again.
Anyways, this problem has been resolved as PhHein has been nice enough to still help me.
- 03-22-2010, 01:17 PM #3
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Actually PhHein found that you've post the question in Suns' forum. Please don't do that next time, stick with a one until you find a better solution. Lots of people, including me, get frustrated about that.
Similar Threads
-
why doesnt my insertion sort method not work?
By Jeremy8 in forum New To JavaReplies: 7Last Post: 11-15-2009, 02:56 AM -
[SOLVED] Insertion Sort in Linked List
By taylorp in forum New To JavaReplies: 10Last Post: 03-27-2009, 12:34 AM -
How to change Bubble+Selection+Insertion Sorts to Sort String Values?
By VinceGuad in forum New To JavaReplies: 3Last Post: 01-26-2009, 12:20 AM -
Insertion Sort in Java
By Java Tip in forum AlgorithmsReplies: 0Last Post: 04-15-2008, 07:41 PM -
Insertion sort algorithm
By Albert in forum Advanced JavaReplies: 2Last Post: 06-28-2007, 08:26 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks