• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to search a name in a group of list

 
soma lingam
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 170
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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,
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic