Results 1 to 8 of 8
  1. #1
    Join Date
    Oct 2009
    Posts
    5
    Rep Power
    0

    Exclamation String comparision method problem

    Hi guys i am given following problem and have to write the program using string comparison methods.....i am also adding my code but i cant get the out put....please help

    Problem:
    Write a program that asks user to enter three names and then display the name in ascending order. For example , if user entered "Charlie" , "Leslie" and "Andy" then the program would display:
    Andy
    Charlie
    Leslie

    Heres what i tried ...i search the whole book but couldn't find how to arrange names in ascending order...
    Java Code:
    import java.util.Scanner;
    
    
    public class SortedNames
    {
       public static void main(String[] args)
       {
       String name1,name2,name3;
    
       Scanner keyboard = new Scanner(System.in);
    
       System.out.print("Enter first name ");
       name1 = keyboard.nextLine();
    
       System.out.print("Enter second name ");
       name2 = keyboard.nextLine();
    
       System.out.print("Enter third name ");
       name3 = keyboard.nextLine();
    
    
       if (name1.compareTo(name2) < 0 )
       {
        System.out.println(name1);
        }
    
      else if (name1.compareTo(name3) < 0)
    	{
    	   System.out.println(name1);
    
        }
     if (name2.compareTo(name1) < 0 )
          {
           System.out.println(name2);
           }
    
     else if (name2.compareTo(name3) < 0)
       	{
       	   System.out.println(name2);
        }
    
    
     if (name3.compareTo(name1) < 0 )
          {
           System.out.println(name3);
           }
    
          else if (name3.compareTo(name1) < 0)
       	{
       	   System.out.println(name3);
        }
    }
    }
    I not getting any error when i complie but when i run and put three names it just displays second and third

    So please hepl me i want them to come in ascending order. Thanks

  2. #2
    CodesAway's Avatar
    CodesAway is offline Senior Member
    Join Date
    Sep 2009
    Location
    Texas
    Posts
    238
    Rep Power
    5

    Default

    Have you checked out / are you allowed to use the built-in sorting functionality?

    Option 1: Store you values in an array and use the static method, Arrays.sort(Object[]).
    Option 2: Store your values in a List and use the static method, Collections.sort(List).

    Regardless which option you choose, if you are allowed to use sorting methods available in Java, it will make your job much easier.

    Just beware that sorting is by ASCII value - so "Leslie" comes before "charlie". If you need a case-insensitive sort, you can either convert all names to lower case (or upper case), or write your own comparator.
    Last edited by CodesAway; 10-14-2009 at 06:06 AM.
    CodesAway - codesaway.info
    writing tools that make writing code a little easier

  3. #3
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Yep, in most of the cases converting the String into one format is much easier to handle, either lower case or upper case. Then process accordingly.

  4. #4
    RamyaSivakanth's Avatar
    RamyaSivakanth is offline Senior Member
    Join Date
    Apr 2009
    Location
    Chennai
    Posts
    835
    Rep Power
    6

    Default

    One more suggestion.Try out using equalsIgnoreCase()..always better
    Ramya:cool:

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by RamyaSivakanth View Post
    One more suggestion.Try out using equalsIgnoreCase()..always better
    or the String#compareToIgnoreCase(...) method here.

  6. #6
    Join Date
    Oct 2009
    Posts
    5
    Rep Power
    0

    Default

    Quote Originally Posted by CodesAway View Post
    Have you checked out / are you allowed to use the built-in sorting functionality?


    Regardless which option you choose, if you are allowed to use sorting methods available in Java, it will make your job much easier.

    Just beware that sorting is by ASCII value - so "Leslie" comes before "charlie". If you need a case-insensitive sort, you can either convert all names to lower case (or upper case), or write your own comparator.
    "Yep, in most of the cases converting the String into one format is much easier to handle, either lower case or upper case. Then process accordingly.
    "

    We cannot use the sorting method just the string comparision .....me so confused


    @ guys i been told to use any case i.e lower or upper
    But using that will get the output...theres nothing to do for the names to come in ascending order????

    Sorry if my questions are dumb but me learning
    Last edited by rons_sacramental; 10-15-2009 at 04:41 AM.

  7. #7
    Join Date
    Oct 2009
    Posts
    5
    Rep Power
    0

    Default

    So can actually anyone tell me the code (not the whole but where i need corrections ) please

  8. #8
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    You have to recode the if-else logic parts actually. Before compare them each other, add all the strings in an array and try to sort them as display it to check the result. Then think about the case conflict in sorting.

Similar Threads

  1. Invoking method from String
    By Supamagier in forum Advanced Java
    Replies: 12
    Last Post: 05-29-2009, 09:21 AM
  2. Replies: 29
    Last Post: 09-25-2008, 07:55 PM
  3. comparision between two lists
    By suprabha in forum Advanced Java
    Replies: 14
    Last Post: 08-01-2008, 02:49 PM
  4. Method for String to int conversion
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 01-02-2008, 06:35 PM
  5. String replace method
    By venkata.tarigopula in forum Advanced Java
    Replies: 1
    Last Post: 07-10-2007, 08:14 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •