DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

+ Reply to Thread
Results 1 to 2 of 2
  1. #1
    Join Date
    Oct 2002
    Posts
    15

    comparing strings

    hi I need to make a program that gets an array list of names, these names are saved as strings,,, but i need the program to sort and print the names in alphabetical order. My professor told us to use the-
    int compareTo(String Str) method, but i do not know how to use it.. any ideas any one??wuwang84@hotmail.com

  2. #2
    Join Date
    Dec 2002
    Posts
    83
    First things first, check out the API at http://java.sun.com/products/jdk/1.2...html#compareTo(java.lang.String)

    The documentation explains it pretty well. Basically you do this:
    Code:
    String strABC = new String("ABC");
    String strDEF = new String("DEF");
    if ( strABC.compareTo(strDEF) == 0 )
    {
    	System.out.println("ABC is the same as DEF");
    }
    else if ( strABC.compareTo(strDEF) < 0 )
    {
    	System.out.println("ABC comes before DEF");
    }
    else {
    	System.out.println("ABC comes after DEF");
    }
    In this case, "ABC comes before DEF" will print because the compareTo() will return a value less than 0.
    -- Steven

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


Top DevX Stories

Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL


Sponsored Links