Results 1 to 3 of 3
  1. #1
    blueduiker is offline Member
    Join Date
    Nov 2009
    Location
    California
    Posts
    55
    Rep Power
    0

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

  2. #2
    blueduiker is offline Member
    Join Date
    Nov 2009
    Location
    California
    Posts
    55
    Rep Power
    0

    Default

    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.

  3. #3
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    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

  1. why doesnt my insertion sort method not work?
    By Jeremy8 in forum New To Java
    Replies: 7
    Last Post: 11-15-2009, 02:56 AM
  2. [SOLVED] Insertion Sort in Linked List
    By taylorp in forum New To Java
    Replies: 10
    Last Post: 03-27-2009, 12:34 AM
  3. Replies: 3
    Last Post: 01-26-2009, 12:20 AM
  4. Insertion Sort in Java
    By Java Tip in forum Algorithms
    Replies: 0
    Last Post: 04-15-2008, 07:41 PM
  5. Insertion sort algorithm
    By Albert in forum Advanced Java
    Replies: 2
    Last Post: 06-28-2007, 08:26 PM

Tags for this Thread

Posting Permissions

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