DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

+ Reply to Thread
Results 1 to 3 of 3
  1. #1
    Join Date
    Mar 2005
    Posts
    6

    How do I do a partial word search?

    Hi all,

    I have a customer's name as an attribute of a reservation object stored in my vector of reservation objects. My current search only does an exact match of a customer name. How do I do a partial word search eg. if my customer name is danielle and I only key in dan, how do i get it to return danielle?

    Hope to hear from someone soon! Thank you!


    Code:
    public void searchcustName(String custName, Vector v)
    {			
           for( int i = 0; i < v.size(); i++) 
           {
               Reservation aReservation = ((Reservation) v.elementAt(i));
               if ((aReservation.getcustName().equals(custName))) 
            	 {System.out.println("Record "+ i + " "+ aReservation.getcustName()+" "+aReservation.getCovers() +" "+ aReservation.getSmoking()+" "+ aReservation.getDate()+" "+aReservation.getstartTime()+" "+aReservation.getendTime()+" "+aReservation.getComments());
            		 searchCount++;
            	}
    }        		
           if (searchCount==0) System.out.println("The record of "+custName+" cannot be found!");
              else  System.out.println("Number of records found: "+searchCount);
            }

  2. #2
    Join Date
    Jan 2005
    Location
    Reisterstowwn, MD
    Posts
    72
    Lucky for you (and all of us) there is a nice little method in the String class called conatins! SO something like:

    Code:
    "danielle".contains("dan");
    would return true. Here is a link to the documentation of this method:

    http://java.sun.com/j2se/1.5.0/docs/api/java/lang/String.html#contains(java.lang.CharSequence)
    Java has 99 problems but a pointer ain't one

  3. #3
    Join Date
    Nov 2004
    Location
    Norway
    Posts
    1,560
    You could also try the startsWith(..) method of the String class

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