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 string.compareTo();

  1. #1
    Junior Member
    Join Date
    Nov 2012
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Thumbs up Problem with string.compareTo();

    I've been having an issue with a lab that I'm doing for school and my professor specifically wants us to use the .compareTo() method for the following code:
    import java.util.*;
     
    public class lab20 {
    	static Scanner in = new Scanner(System.in);
    	public static void main(String[] args)
    	{
    		String[] names = new String[20];
    		int size = 0;
     
    		System.out.print("Enter a name, last name first(Enter 'END' to quit): ");
    		String name = in.nextLine();
    		while (!name.equals("END") && names.length <= 20)
    		{
    			names[size] = name;
    			System.out.print("Enter another name: ");
    			name = in.nextLine();
    			size++;
    		}
    		for (int i = 0; i < size; i++)
    			System.out.println(names[i]);
    		sort(names, size);
     
    		System.out.println();
     
    		for (int i = 0; i <size; i++)
    			System.out.println(names[i]);
    	}
    	public static void sort (String[] names, int size)
    	{
    		int i = 0;
    		int minPos;
    		String temp;
     
    		for (i = 0; i < size; i++)
    		{
    			minPos = minimunPosition(names, i, size);
    			temp = names[i];
    			names[i] = names[minPos];
    			names[minPos] = temp;
    		}
    	}
    	public static int minimunPosition(String[] names, int i, int size)
    	{
    		int minPos = i;
    		int k;
    		for (k = i+1; k < size; k++)
    		{
    			if (names[k].compareTo(names[minPos]) == -1)
    				minPos = k;
    		}
    		return minPos;
    	}
     
    }
    The input that I'm doing is:
    Strickland, Bobby
    Anderson, Brett
    Keefer, Chris
    Whymer, Josh
    END
    the output is the same as above without the END, obviously. I've tried using 1 instead of -1 as the return value of compareTo() but it's stays the same. I've changed the if statement to (name[k].charAt(0) < name[minPos].charAt(0)) but my professor said he'd take off points because he specifically want's me to use the compareTo(). Any help is greatly appreciated.


  2. #2
    Junior Member
    Join Date
    Oct 2012
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default

    Hey,
    The code shows that u r assinging names to temp variable...
    how u can assign a string array to string variable?

    Apart from this, every thing looks correct to me.

  3. #3
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: Problem with string.compareTo();

    @purvesh: I haven't seen him assigning the array to a variable.

    @Drasumok: Please provide the exact question you need help with? What is the actual input, what is the output? In case of errors or exceptions, post here the full exception trace to get help.
    Anyone who stops learning is old, whether at twenty or eighty. Anyone who keeps learning stays young. The greatest thing in life is to keep your mind young.

    - Henry Ford

  4. #4
    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: Problem with string.compareTo();

    When does compareTo() return a -1? Where does the API say that value is returned?
    If you don't understand my answer, don't ignore it, ask a question.

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

    purvesh (November 30th, 2012)

  6. #5
    Junior Member
    Join Date
    Oct 2012
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default

    Exactly!! Change d condition to less then zero
    Also use compareToIgnoreCase instead of compareTo and compare with names[i] instead of names[minPos]

  7. #6
    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: Problem with string.compareTo();

    Please mark this thread as solved if the problem is solved.
    If you don't understand my answer, don't ignore it, ask a question.

  8. #7
    Junior Member
    Join Date
    Nov 2012
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Problem with string.compareTo();

    Quote Originally Posted by Mr.777 View Post
    @purvesh: I haven't seen him assigning the array to a variable.

    @Drasumok: Please provide the exact question you need help with? What is the actual input, what is the output? In case of errors or exceptions, post here the full exception trace to get help.
    Below the code is where I put the input and output of the program, and setting the compareTo() < 0 fixed my problem

Similar Threads

  1. [SOLVED] comparing objects using compareTo
    By jslice3 in forum Object Oriented Programming
    Replies: 3
    Last Post: November 19th, 2012, 10:09 PM
  2. Implementing the compareTo method?
    By colerelm in forum Java Theory & Questions
    Replies: 2
    Last Post: December 3rd, 2011, 07:47 PM
  3. string==null or string.equals(null) problem
    By csharp100 in forum What's Wrong With My Code?
    Replies: 31
    Last Post: November 4th, 2011, 08:17 AM
  4. Help Me with my Program CompareTo!!!!
    By WantHelp in forum What's Wrong With My Code?
    Replies: 14
    Last Post: July 7th, 2011, 12:54 PM
  5. Replies: 3
    Last Post: June 1st, 2011, 12:47 AM