File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Java in General and the fly likes How to search a name in a group of list Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "How to search a name in a group of list" Watch "How to search a name in a group of list" New topic

How to search a name in a group of list

soma lingam

Joined: Apr 26, 2007
Posts: 22
Hi all,

I had a problem relating to search the name in a given 10 names like for instance
if there are 10 fields that are added in arraylist like :-


if i want to search for a name called "raghu" i need to display the whole
contents i.e., for example:- if i found raghu in "RameshraghuRakesh" this
word has to be displayed
either by giving a short name like "rag" or by giving like "%raghu" the whole contents has to be displayed.

Please, help me in this regard, with an example:-
I am awaiting for your replies

vanlalhmangaiha khiangte
Ranch Hand

Joined: Sep 11, 2006
Posts: 170
ArrayList list = new ArrayList();

String searchitem="rag";//Search item
for(int i=0;i<list.size();i++)
String temp=(String)list.get(i);
System.out.println(" "+temp);//printing out the names

For the special characters like '%','*' i think that you need to write the logic for it
Campbell Ritchie

Joined: Oct 13, 2005
Posts: 45255
Welcome to the Ranch, both of you.

Please use "code" before posting code; it makes it easier to read.

What you are actually trying to do is to match a regular expression. A little confusingly, you have between you got three regular expressions you wish to match, "raghu", "rag" and "%raghu". Go through the Java Tutorial here. You can probably alter your regular expressions so "raghu" matches anywhere in the Strings and "%raghu" matches the String but not the first position.

Suggest you don't use an unparameterised ArrayList. Better to use a generic list, and use an Iterator rather than a for loop:
soma lingam

Joined: Apr 26, 2007
Posts: 22
Thanks for giving the reply ofcourse, i implemented this one but i need
to know how i need to get the list of information by typing as %raghu or

Help me in this regard,
I agree. Here's the link:
subject: How to search a name in a group of list
It's not a secret anymore!