Ad Home

Theme images by kelvinjay. Powered by Blogger.

Travel

Header Ads

Java

Selenium

UFT

Framework

General QA Concept

BDD

» » » How To Code Advanced Search In Java

In my last post, I have shown how to code for How To Find Occurrence of A Word in a String. In this post we will see how we can do an advanced Search with Java.

Say I have a string or or few names and I want to find out how many words starts with a particular pattern.

Example-

Say I have a list of names-"Animesh","Anitha","Arindam","Arijeet"
and I want to found out the count of names that starts with "Ani".

So my algorithm will be get all names in an array and check the first 3 characters of that.

 
These functions will ease the search functionality, save a lot of extra effort in terms of coding.

If it matches with "Ani"  then increment counter by one.
Well Java has come up with couple of good functions..
  • startsWith
  • endsWith 




public int startw(String String_,String Search_ ) {
String g=String_;
String str=Search_;
if(String_.startsWith(str)) {
count++;
}
return count;
}
Let me create the main method..
public static void main(String[] args) {
int val=0;
SearchinString searchinString = new SearchinString();
String[] names={"Animesh","Anitha","Arindam","Arijeet"};
String src="Ani";
for(String n:names )
{
val=searchinString.startw(n,src);

}
System.out.println(" The count "+val);
}

Lets us also do some more searching activities...Lets find out count of the names which end with "eet"


public int endw(String String_,String Search_ ) {
String g=String_;
String str=Search_;
if(String_.endsWith(str)) {
count1++;
}
return count1;
}


So the over all coding goes like-


public class SearchinString {
int count=0;
int count1=0;
public int startw(String String_,String Search_ ) {
String g=String_;
String str=Search_;
if(String_.startsWith(str)) {
count++;
}
return count;
}
public int endw(String String_,String Search_ ) {
String g=String_;
String str=Search_;
if(String_.endsWith(str)) {
count1++;
}
return count1;
}

public static void main(String[] args) {
int val=0;
int val1=0;
SearchinString searchinString = new SearchinString();
String[] names={"Animesh","Anitha","Arindam","Arijeet","Prosenjeet","Bipinjeet"};
String src="Ani";
String src1="eet";
for(String n:names )
{
val=searchinString.startw(n,src);

}
System.out.println(" The count "+val);
for(String n:names )
{
val1=searchinString.endw(n,src1);

}
System.out.println(" The count "+val1);
}
}


Now let us more customize it..
Let us take user inputs here...You can check out this tutorial how to get user input
here.

String str = null;
str = JOptionPane.showInputDialog(null, "What is your String?", "Enter your String?", JOptionPane.QUESTION_MESSAGE);

String srchstr = null;
srchstr = JOptionPane.showInputDialog(null, "What is your Search word?", "Enter your starting word to be searched?", JOptionPane.QUESTION_MESSAGE);

String srchstr1 = null;
srchstr1 = JOptionPane.showInputDialog(null, "What is your Search word?", "Enter your ending word to be searched?", JOptionPane.QUESTION_MESSAGE);

The final code goes like below ..


import javax.swing.JOptionPane;

public class SearchinString {
int count=0;
int count1=0;
public int startw(String String_,String Search_ ) {
String g=String_;
String str=Search_;
if(String_.startsWith(str)) {
count++;
}
return count;
}
public int endw(String String_,String Search_ ) {
String g=String_;
String str=Search_;
if(String_.endsWith(str)) {
count1++;
}
return count1;
}

public static void main(String[] args) {
int val=0;
int val1=0;
String str = null;
str = JOptionPane.showInputDialog(null, "What is your String?", "Enter your String?", JOptionPane.QUESTION_MESSAGE);
//String str="I am a good boy. I am a bad boy";
String srchstr = null;
srchstr = JOptionPane.showInputDialog(null, "What is your Search word?", "Enter your starting word to be searched?", JOptionPane.QUESTION_MESSAGE);

String srchstr1 = null;
srchstr1 = JOptionPane.showInputDialog(null, "What is your Search word?", "Enter your ending word to be searched?", JOptionPane.QUESTION_MESSAGE);

SearchinString searchinString = new SearchinString();
String Ori=str;
String[] splits = Ori.split(" ");


// String[] names={"Animesh","Anitha","Arindam","Arijeet","Prosenjeet","Bipinjeet"};
// String src="Ani";
//String src1="eet";
for(String n:splits )
{
val=searchinString.startw(n,srchstr);

}
System.out.println(" The count "+val);
for(String n:splits )
{
val1=searchinString.endw(n,srchstr1);

}
System.out.println(" The count "+val1);
}
}


It will empower users to get the string, the starting string and ending string . It will also find out the count of different type of search.

That it from my side. Please comment below if you likes it.:)

«
Next
Newer Post
»
Previous
Older Post

3 comments: