Spread the love

How To Code Advanced Search In Java

Spread the love

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.:)

READ  Garbage Collector Simplified in Java
READ  How To Convert A Picture to GrayScale Image In Java




Spread the love
Animesh Chatterjeehttps://techtravelhub.com/
I am the founder and owner of the blog - TechTravelHub.com, always love to share knowledge on test automation,tools,techniques and tips.I am a passionate coder of Java and VBScript.I also publish articles on Travel ideas and great honeymoon destinations.Apart from these, I am a gear-head,love to drive across India. I have shared lots of articles here on How to travel several parts of India.Customization of cars aka car modification is my another hobby.Get in touch with me on ani01104@gamil.com

Related Articles

Super Keyword in Java Simplified

Super Keyword Super Keyword in Java Simplified: Java defines a special keyword called...

Comments

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Recent Posts

Super Keyword in Java Simplified

Super Keyword Super Keyword in Java Simplified: Java defines a special...

Learn Object Repository in UFT in An Efficient and Fast Manner

Object Repository in UFT The post will cover the following...

Learn What Primary Tools For Test Automation

Primary Tools For Test Automation According to the Gartner report...

EDITOR PICKS


Spread the love