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: Problem with sorting by alphabetical order

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

    Unhappy Problem with sorting by alphabetical order

    Hello, I have started to learn java for about 3 months, thus I don't know much about it. I want to write a short code that can sort an array of name by alphabetical order. I did it in two different ways, the 1st one worked, but the second ways did not. Would you please help me to correct the problem. Thanks a lot.

    This is the one that worked

    import java.util.*;
      public class testsortname{
      public static void main (String args[]){
     
        String s1= "Jerry";String s2= "AAA";String s3= "BTom";String s4= "AATom";String s5= "TDom";
        String s6= "BlueBerry";String s7= "Samson";String s8= "Rinor";String s9= "Aladin";String s10= "FarFar";
        String[] test=new String[10];
        test[0]=s1;test[1]=s2;test[2]=s3;test[3]=s4;test[4]=s5;
      test[5]=s6;test[6]=s7;test[7]=s8;test[8]=s9;test[9]=s10;
        for(int j=0;j<test.length;j++){
          System.out.println(test[j]);}
     
      System.out.println(s1.compareTo(s2));
      System.out.println(s2.compareTo(s3));
     
      int i=1; String[] b=new String[test.length];b[0]=test[0];
     
      while (i<test.length){
      int k = i-1;boolean condition = false;
      while(k>=0&&condition==false){
        if(b[k].compareTo(test[i])<=0){
          b[k+1]=test[i];condition=true;
        }
        else{b[k+1]=b[k];b[k]=test[i];k--;}
      }
      i++;}
     
      System.out.println("--------------------------------");
     
      for(int j=0;j<b.length;j++){
      System.out.println(b[j]);
      }
     
      }
     
      }


    This is the one that didn't


    import java.util.*;
      public class testsortname2{
      public static void main (String args[]){
     
        String s1= "Jerry";String s2= "AAA";String s3= "BTom";String s4= "AATom";String s5= "TDom";
        String s6= "BlueBerry";String s7= "Samson";String s8= "Rinor";String s9= "Aladin";String s10= "FarFar";
        String[] test=new String[10];
        test[0]=s1;test[1]=s2;test[2]=s3;test[3]=s4;test[4]=s5;
      test[5]=s6;test[6]=s7;test[7]=s8;test[8]=s9;test[9]=s10;
     
      for(int j=0;j<test.length;j++){
          System.out.println(test[j]);}
     
      String[] temp=new String[test.length];
      temp[0]=test[0];
     
      for(int i=1;i<10;i++){
      int k=0;
      while(k<=i&&(test[i].compareTo(temp[k])>0)){k++;}
     
      for(int j=10;j<=k+1;j--){temp[j]=temp[j-1];}
      temp[k]=test[i];
     
      }
     
      System.out.println("--------------------------------");
     
      for(int j=0;j<temp.length;j++){
      System.out.println(temp[j]);
     
      }}}


  2. #2
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    939
    Thanks
    6
    Thanked 202 Times in 178 Posts

    Default Re: Problem with sorting by alphabetical order

    Why - in what way - did the second version not work?

    Did it not compile? If so, post the compiler's messages if you can't understand them and say which lines of your code they refer to.

    Did it throw an exception at runtime? If so, post the complete stack trace (the long message you get when an exception happens) and, again, say what lines of your code it is referring to.

    Did it show some other unwanted behaviour at runtime? If so, say what you expected would be output and what you actually observed.

    -----

    The code could be tidied up somewhat. Spaces (horizontal and new lines) cost nothing and they provide the eye with information about logical grouping. Blocks of code, especially, should be indented (and in a consistent manner). Consider descriptive variable names as these also carry with them an expression of your intent.

  3. The Following User Says Thank You to pbrockway2 For This Useful Post:

    pikapika (April 6th, 2012)

  4. #3
    Member
    Join Date
    Jan 2012
    Location
    Hellas
    Posts
    284
    Thanks
    11
    Thanked 59 Times in 57 Posts

    Default Re: Problem with sorting by alphabetical order

    Quote Originally Posted by pikapika View Post
    Hello, I have started to learn java for about 3 months, thus I don't know much about it. I want to write a short code that can sort an array of name by alphabetical order. I did it in two different ways, the 1st one worked, but the second ways did not. Would you please help me to correct the problem. Thanks a lot.

    This is the one that didn't


    import java.util.*;
      public class testsortname2{
      public static void main (String args[]){
     
        String s1= "Jerry";String s2= "AAA";String s3= "BTom";String s4= "AATom";String s5= "TDom";
        String s6= "BlueBerry";String s7= "Samson";String s8= "Rinor";String s9= "Aladin";String s10= "FarFar";
        String[] test=new String[10];
        test[0]=s1;test[1]=s2;test[2]=s3;test[3]=s4;test[4]=s5;
      test[5]=s6;test[6]=s7;test[7]=s8;test[8]=s9;test[9]=s10;
     
      for(int j=0;j<test.length;j++){
          System.out.println(test[j]);}
     
      String[] temp=new String[test.length];
      temp[0]=test[0];
     
      for(int i=1;i<10;i++){
      int k=0;
      while(k<=i&&(test[i].compareTo(temp[k])>0)){k++;}
     
      for(int j=10;j<=k+1;j--){temp[j]=temp[j-1];}
      temp[k]=test[i];
     
      }
     
      System.out.println("--------------------------------");
     
      for(int j=0;j<temp.length;j++){
      System.out.println(temp[j]);
     
      }}}
    Hello pikapika!
    It would help if you explained what you got (errors, exceptions, unexpected behaviour) when you ran your program. But as i can see you will probably face a NullPointerException in
    while (k <= i && (test[i].compareTo(temp[k]) > 0)) {
                    k++;
                }
    because you only assign a value to temp[0] and at some point you will check temp[1], temp[2],....
    Hope it helps.

  5. The Following User Says Thank You to andreas90 For This Useful Post:

    pikapika (April 6th, 2012)

  6. #4
    Junior Member
    Join Date
    Mar 2012
    Posts
    3
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Problem with sorting by alphabetical order

    Quote Originally Posted by andreas90 View Post
    Hello pikapika!
    It would help if you explained what you got (errors, exceptions, unexpected behaviour) when you ran your program. But as i can see you will probably face a NullPointerException in
    while (k <= i && (test[i].compareTo(temp[k]) > 0)) {
                    k++;
                }
    because you only assign a value to temp[0] and at some point you will check temp[1], temp[2],....
    Hope it helps.
    Yes, that's the error I got, the NullPointerException and I did not understand what was going on. Thanks for trying to help

    @ pbrockway2 : Thanks for trying to help


    This is the error message:

    java.lang.NullPointerException
    at java.lang.String.compareTo(Unknown Source)
    at testsortname2.main(testsortname2.java:19)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Nativ e Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknow n Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Un known Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at edu.rice.cs.drjava.model.compiler.JavacCompiler.ru nCommand(JavacCompiler.java:271)

  7. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,186
    Thanks
    49
    Thanked 2,172 Times in 2,145 Posts

    Default Re: Problem with sorting by alphabetical order

    java.lang.NullPointerException
    at java.lang.String.compareTo(Unknown Source)
    at testsortname2.main(testsortname2.java:19)
    what variable has a null value on line 19? If you can not see which one it is, add a println to print out the values of all the variables used on line 19. When you find the variable you need to look back in the code to see why that variable does not have a valid value.
    If you don't understand my answer, don't ignore it, ask a question.

  8. The Following User Says Thank You to Norm For This Useful Post:

    pikapika (April 6th, 2012)

  9. #6
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    939
    Thanks
    6
    Thanked 202 Times in 178 Posts

    Default Re: Problem with sorting by alphabetical order

    [Edit] Norm's answer is a better way to go...
    Last edited by pbrockway2; April 6th, 2012 at 08:51 PM. Reason: second thoughts

  10. #7
    Junior Member
    Join Date
    Mar 2012
    Posts
    3
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Problem with sorting by alphabetical order

    Thanks guys, I figure it out

    while(k<=i&&(test[i].compareTo(temp[k])>0)){k++;}

    for(int j=10;j<=k+1;j--){temp[j]=temp[j-1];}
    temp[k]=test[i];
    should be changed to

    while(k<i&&(test[i].compareTo(temp[k])>0)){k++;}
    for(int j=9;j>=k+1;j--){temp[j]=temp[j-1];}
    temp[k]=test[i];
    because there are 10 cells in the array, thus the max index is 9, and j should be greater than/ equal to k+1.
    Last edited by pikapika; April 6th, 2012 at 10:01 PM.

Similar Threads

  1. RPN Help Please! (Order of Operations)
    By yuvalb in forum What's Wrong With My Code?
    Replies: 6
    Last Post: September 30th, 2011, 10:53 AM
  2. Sorting problem
    By Kaisshau in forum What's Wrong With My Code?
    Replies: 9
    Last Post: June 28th, 2011, 10:05 PM
  3. Threads in some order...
    By aps135 in forum Threads
    Replies: 6
    Last Post: March 11th, 2011, 04:54 PM
  4. Sort in Cyrilic order
    By cselic in forum What's Wrong With My Code?
    Replies: 3
    Last Post: May 5th, 2010, 03:08 PM
  5. Stack Order?
    By TimW in forum AWT / Java Swing
    Replies: 2
    Last Post: September 19th, 2009, 07:33 AM