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

Thread: Writing my own custom comparator in java

  1. #1
    Member
    Join Date
    Jun 2014
    Posts
    77
    Thanks
    14
    Thanked 0 Times in 0 Posts

    Default Writing my own custom comparator in java

    Hello guys,

    I have a java code that should sort an array of names based on the last name. e.g jane a, jane b, jane z, jane d should be
    jane a, jane b, jane d, jane z. I have the following code but for some reasons, the s1 in the comparator method is always null. Any idea why this is so?


     
     
     
    public class ShuffleName {
     
     
    	public static void sortNames(String[] names){
     
    		 Arrays.sort( names, new Comparator<String>() { 
     
    				public int compare( String s1, String s2 ) {
     
    				String s1last = s1.split("\\s+")[1]; 
    				String s2last = s2.split("\\s+")[1]; 
    				return s1last.compareTo(s2last); 
    				} 
    				}); 
     
    				for ( String s : names ) System.out.println(s);
     
     
     
     
    	}
     
     
    	public static void main(String[] args) {
     
    		System.out.println("Enter your firstName and sureName : \n");
    		Scanner st = new Scanner(System.in);
    		String [] namesArray = new String[6];
    		String name = new String();
    		int index = 0;
    		int maxLength = namesArray.length;
     
    		name = st.nextLine();
     
    		while( (!name.trim().equals(""))){
     
    				if (index < maxLength) {
    					namesArray[index] = name;
    					index++;
    				} else {
    					namesArray = Arrays.copyOf(namesArray, maxLength + 4);
    					namesArray[index] = name;
    					index++;
    				}
     
    				System.out.println("Enter your firstName and sureName : \n");
    				name = st.nextLine();
    		}
     
     
    		sortNames(namesArray);
     
     
     
    	}
     
    }


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Writing my own custom comparator in java

    Post a sample run copied from your console that shows the inputs/outputs and the problem you've described. If you're asking for help with an error message, post the complete text of the error message.

  3. #3
    Member
    Join Date
    Jun 2014
    Posts
    77
    Thanks
    14
    Thanked 0 Times in 0 Posts

    Default Re: Writing my own custom comparator in java

    INPUT:
     
    Enter your firstName and sureName : 
    john a [ENTER]
     
    Enter your firstName and sureName : 
    john b [ENTER]
     
    Enter your firstName and sureName : 
    john z [ENTER]
     
    Enter your firstName and sureName : 
    john d [ENTER]
     
    Enter your firstName and sureName : 
    [ENTER]
     
     
    OUTPUT:
     
    Exception in thread "main" java.lang.NullPointerException
    	at org.Busy.WeYou.ShuffleName$1.compare(ShuffleName.java:16)
    	at org.Busy.WeYou.ShuffleName$1.compare(ShuffleName.java:1)
    	at java.util.TimSort.binarySort(TimSort.java:265)
    	at java.util.TimSort.sort(TimSort.java:190)
    	at java.util.TimSort.sort(TimSort.java:173)
    	at java.util.Arrays.sort(Arrays.java:659)
    	at org.Busy.WeYou.ShuffleName.sortNames(ShuffleName.java:12)
    	at org.Busy.WeYou.ShuffleName.main(ShuffleName.java:57)
    The reason is because the String s1 is null even though when I checked the names array, all the names entered are complete.
    Last edited by help_desk; July 27th, 2014 at 05:02 PM. Reason: format line

  4. #4
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Writing my own custom comparator in java

    You have an array of names that looks something like:

    [ john a, john b, john z, john d, null, null ]

    How will the null values be handled?

  5. #5
    Member
    Join Date
    Jun 2014
    Posts
    77
    Thanks
    14
    Thanked 0 Times in 0 Posts

    Default Re: Writing my own custom comparator in java

    Quote Originally Posted by GregBrannon View Post
    You have an array of names that looks something like:

    [ john a, john b, john z, john d, null, null ]

    How will the null values be handled?
    Yeah that is true but internally the array seems to be something like this :
    [ null, john b, john z, john d, null, null ]

    I don't get why this is so. Please, how could I handle null in my custom comparator? I could do something like this:

    public int compare( String s1, String s2 ) {
                                    if(s1!=null && s2!=null){
    				String s1last = s1.split("\\s+")[1]; 
    				String s2last = s2.split("\\s+")[1]; 
    				return s1last.compareTo(s2last); 
    				} 
                             }

    but that still doesn't explain why the s1 String is null even though, that was not how the names were stored.

  6. #6
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Writing my own custom comparator in java

    You can add some print statements to see what is happening and answer your question. For example, I added a few and got the following as a result. From that you can see it's not as simple as you're imagining.
    Enter your firstName and sureName : 
     
    John a
    Name = John a
    Enter your firstName and sureName : 
     
    John b
    Name = John b
    Enter your firstName and sureName : 
     
    John z
    Name = John z
    Enter your firstName and sureName : 
     
    John d
    Name = John d
    Enter your firstName and sureName : 
     
     
    Array of names = [John a, John b, John z, John d, null, null]
    s1 = John b
    s2 = John a
    s1 = John z
    s2 = John b
    s1 = John d
    s2 = John z
    s1 = John d
    s2 = John b
    s1 = John d
    s2 = John z
    s1 = null
    s2 = John d
    Exception in thread "main" java.lang.NullPointerException
    	at TestClass$1.compare(TestClass.java:16)
    	at TestClass$1.compare(TestClass.java:1)
    	at java.util.TimSort.binarySort(TimSort.java:265)
    	at java.util.TimSort.sort(TimSort.java:190)
    	at java.util.TimSort.sort(TimSort.java:173)
    	at java.util.Arrays.sort(Arrays.java:659)
    	at TestClass.sortNames(TestClass.java:9)
    	at TestClass.main(TestClass.java:60)

  7. The Following User Says Thank You to GregBrannon For This Useful Post:

    help_desk (July 27th, 2014)

Similar Threads

  1. Java Custom font
    By Trolltrain in forum Java Theory & Questions
    Replies: 0
    Last Post: July 14th, 2014, 05:53 AM
  2. [SOLVED] Sorting with a comparator
    By keepStriving in forum Java Theory & Questions
    Replies: 6
    Last Post: March 26th, 2014, 02:09 PM
  3. Problem with Comparator
    By iHank in forum What's Wrong With My Code?
    Replies: 4
    Last Post: November 30th, 2013, 06:41 AM
  4. When to use Comparator and Comparable Interface in java
    By diyaots in forum Java Theory & Questions
    Replies: 7
    Last Post: October 26th, 2012, 09:35 AM
  5. [SOLVED] help with sorting...(comparator)
    By mdstrauss in forum Collections and Generics
    Replies: 2
    Last Post: July 26th, 2009, 06:25 AM

Tags for this Thread