# Thread: problem with insertion sort???

1. Member
Join Date
Nov 2009
Location
California
Posts
55
Rep Power
0

## problem with insertion sort???

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```
Basically when I use my client program the output looks like this:
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 Wizard.<init>(Wizard.java:41)

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?

2. Member
Join Date
Nov 2009
Location
California
Posts
55
Rep Power
0

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.

3. 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.