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 7 of 7

Thread: Insertion Sort with String Array error in the While loop at the bottom of the code

  1. #1
    Junior Member
    Join Date
    Mar 2013
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Insertion Sort with String Array error in the While loop at the bottom of the code

    package InsertionSort;

    public class InsertionSort {

    public static void main(String a[]){
    int i;
    String array[] = {"I","N","S","E","R","T","I","O","N"};

    System.out.println("Before sort:\n");
    for(i = 0; i < array.length; i++){
    System.out.print( array[i]+" "); }
    System.out.println();


    insertion_srt(array, array.length);

    System.out.print("After sort:\n");
    for(i = 0; i <array.length; i++){
    System.out.print(array[i]+" ");
    System.out.println();}


    }


    public static void insertion_srt(String array[], int z){
    for (int i = 1; i < z; i++){
    int j = i;
    String B = array[i];
    while ((j > 0) && (array[j-1] > B)){
    array[j] = array[j-1];
    j--;
    }
    array[j] = B;
    }
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,346
    Thanks
    56
    Thanked 2,376 Times in 2,346 Posts

    Default Re: Insertion Sort with String Array error in the While loop at the bottom of the code

    Please copy the full text of the error message and paste it here.

    Please edit your post and wrap your code with
    [code=java]
    <YOUR CODE HERE>
    [/code]
    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Mar 2013
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Insertion Sort with String Array error in the While loop at the bottom of the code

    package InsertionSort;
     
    public class InsertionSort {
     
    public static void main(String a[]){
    int i;
    String array[] = {"I","N","S","E","R","T","I","O","N"};
     
    System.out.println("Before sort:\n");
    for(i = 0; i < array.length; i++){
    System.out.print( array[i]+" "); }
    System.out.println();
     
    insertion_srt(array, array.length);
     
    System.out.print("After sort:\n");
    for(i = 0; i <array.length; i++){
    System.out.print(array[i]+" ");
    System.out.println();}
     
    }
     
    public static void insertion_srt(String array[], int z){
    for (int i = 1; i < z; i++){
    int j = i;
    String B = array[i];
    while ((j > 0) && (array[j-1] > B)){
    array[j] = array[j-1];
    j--;
    }
    array[j] = B;
    }
    }

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,346
    Thanks
    56
    Thanked 2,376 Times in 2,346 Posts

    Default Re: Insertion Sort with String Array error in the While loop at the bottom of the code

    Please copy the full text of the error message and paste it here.

    Please edit the posted code and give it proper formatting. Nested statements should be indented 3-4 spaces.
    All statements should NOT start in the first column.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Mar 2013
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Insertion Sort with String Array error in the While loop at the bottom of the code

    The error I get says:

    bad operand types for binary operator '>'
    first type: java.lang.String
    second type: java.lang.String
    ----
    (Alt-Enter shows hints)

    Thank you for looking at the code.
    I'm new here and I'm a beginner.
    Here is the code below:
    package InsertionSort;
     
    public class InsertionSort {
     
    public static void main(String a[]){
    String array[] = {"I","N","S","E","R","T","I","O","N"};
    int i;
     
    System.out.println("Before sort:\n");
    for(i = 0; i < array.length; i++){
        System.out.print( array[i]+" "); }
        System.out.println();
     
    insertion_srt(array, array.length);
     
    System.out.print("After sort:\n");
    for(i = 0; i <array.length; i++){
        System.out.print(array[i]+" ");
        System.out.println();}
     }
     
    public static void insertion_srt(String array[], int z){
    for (int i = 1; i < z; i++){
        int j = i;
        String B = array[i];
        while ((j > 0) && (array[j-1] > B)){
            array[j] = array[j-1];
            j--;}
        array[j] = B;}
    }
    }

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,346
    Thanks
    56
    Thanked 2,376 Times in 2,346 Posts

    Default Re: Insertion Sort with String Array error in the While loop at the bottom of the code

    bad operand types for binary operator '>'
    first type: java.lang.String
    second type: java.lang.String
    The compiler doesn't allow the > operator for comparing Strings. You need to use a method when comparing Strings.
    See the String class's API doc for method(s) to use to compare Strings.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Member Zyrion's Avatar
    Join Date
    Feb 2013
    Location
    Iowa
    Posts
    106
    My Mood
    Angelic
    Thanks
    2
    Thanked 8 Times in 8 Posts

    Default Re: Insertion Sort with String Array error in the While loop at the bottom of the code

    ^ Beat me to it.

Similar Threads

  1. how to sort array by object string member value?
    By amgleason83 in forum Collections and Generics
    Replies: 9
    Last Post: March 16th, 2012, 06:29 PM
  2. Insertion Sort - memory usage
    By lovon in forum Algorithms & Recursion
    Replies: 2
    Last Post: November 6th, 2011, 04:00 PM
  3. 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
  4. Insertion Sort
    By Kimimaru in forum Algorithms & Recursion
    Replies: 2
    Last Post: December 6th, 2010, 06:26 AM
  5. Pseudo code of insertion sort linked list
    By sungirl in forum Algorithms & Recursion
    Replies: 1
    Last Post: May 23rd, 2010, 10:25 AM

Tags for this Thread