wood burning stoves 2.0*
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


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
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
Author

How to search a name in a group of list

soma lingam
Greenhorn

Joined: Apr 26, 2007
Posts: 19
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 :-

list.add("raghu");
list.add("RameshraghuRakesh");
list.add("srinuraghu");
list.add("ramurameshraghu");

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


Thanks&Regards,
Soma
vanlalhmangaiha khiangte
Ranch Hand

Joined: Sep 11, 2006
Posts: 170
ArrayList list = new ArrayList();
list.add("raghu");
list.add("RameshraghuRakesh");
list.add("srinuraghu");
list.add("ramurameshraghu");
list.add("abc");
list.add("def");
list.add("ghi");
list.add("lmno");

String searchitem="rag";//Search item
for(int i=0;i<list.size();i++)
{
String temp=(String)list.get(i);
if(temp.indexOf(searchitem)>-1)
{
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
Sheriff

Joined: Oct 13, 2005
Posts: 39478
    
  28
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
Greenhorn

Joined: Apr 26, 2007
Posts: 19
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
%rag

Help me in this regard,
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to search a name in a group of list