comparing strings


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 2 of 2

Thread: comparing strings

  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

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


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center