Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Need help with insertion method

  1. #1
    Member
    Join Date
    Dec 2012
    Location
    Detroit Mi
    Posts
    122
    My Mood
    Amazed
    Thanks
    14
    Thanked 0 Times in 0 Posts

    Default Need help with insertion method

    Here is my assignment.

    Use the code samples that you created in class for this project.
    Load the following items of data into an array interactively from the keyboard.
    Print out the data in the order in which is was entered.
    Sort the data in ascending order using the insertion sort.
    Print out the data in sorted order.
    Sort the data in descending order using the bubble sort.
    Print out the data in the sorted order.
    These are the data items you’ll use:
    Honda
    Chevrolet
    Ford
    Rolls-Royce
    Mercedes Benz

    I'm having trouble with the insertion method when running my methods in my main method. Here is my output.
    Hello user, I will sort your list in asscending and descending order?
    You will need to input 5 strings. Please press enter for each string.
    Please enter your string.
    Honda
    Chevrolet
    Ford
    Rolls-Royce
    Mercedes Benz


    Honda


    Here is my code

     
    import java.util.*;
     
    public class Project7
    {
        public static void main (String[] args)
        {
             Scanner scan = new Scanner(System.in);
             String[] storedArray = new String[5];
     
     
             int size = 5;
             int i;
     
     
            System.out.println( "Hello user, I will sort your list in asscending and descending order?  ");
            System.out.println( "You will need to input 5 strings. Please press enter for each string." );
            System.out.println( "Please enter your string." );
     
             for(i = 0; i < storedArray.length; i++)
             {
                 storedArray[i] = scan.nextLine();
     
             }
     
             insertionSort(storedArray,size);
             printArray(storedArray);
     
          }
     
     
         public static void insertionSort(String[] array, int length)
         {
                int i;
                for( i = 1; i<length; i++)
                acsendinginsert(array,i);
     
         }
     
         public static void acsendinginsert(String[] x, int i)
         {
            String temp = " ";
            int j, result, result1;
     
            j = i-1;
            result = x[i].compareToIgnoreCase(x[j]);
            result1 = x[j].compareToIgnoreCase(x[i]);
            while(j >= 0 && result < result1 )
            {
                x[j+1] = x[j];
                j--;
            }
            x[j+1] = temp; 
     
         }
     
         public static void printArray(String displayed [])
         {
            int i;
     
            for(i = 0; i < displayed.length; i++)
            {
                System.out.println(displayed[i]);
            }
     
     
         } 
     
     
        }

    For some reason my output is only returning one string instead of my list of order strings. Is there any suggestions?


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Need help with insertion method

    Please copy the program's output and paste it here. Add some comments saying what is wrong and show what it should be.

    Try debugging the code by adding some println statements that show the contents of the array during items being added to it, before the sort and after the sort so you can see what the code is doing.
    Here's an easy way to print an array:
     System.out.println("an ID "+ java.util.Arrays.toString(theArrayName));
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member
    Join Date
    Dec 2012
    Location
    Detroit Mi
    Posts
    122
    My Mood
    Amazed
    Thanks
    14
    Thanked 0 Times in 0 Posts

    Default Re: Need help with insertion method

    I was able to figure it out. Thanks for the response.

Similar Threads

  1. Insertion sorts
    By maple1100 in forum What's Wrong With My Code?
    Replies: 0
    Last Post: March 28th, 2013, 09:23 PM
  2. Using a Generic Insertion Sort method
    By dubois.ford in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 6th, 2011, 06:08 PM
  3. Insertion Sort
    By Kimimaru in forum Algorithms & Recursion
    Replies: 2
    Last Post: December 6th, 2010, 06:26 AM
  4. Two insertion into RDBMS
    By nrao in forum JDBC & Databases
    Replies: 4
    Last Post: November 11th, 2010, 08:13 PM
  5. [SOLVED] Sorting a vector using insertion
    By DM21 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: October 25th, 2010, 05:36 PM