Wednesday, November 18, 2009

To Demonstrate Searching the word

PROBLEM STATEMET
Create an application in j2me to demonstrate that will search for a word present in the given paragraph with 2 options:
a. case sensitive
b. case insensitive

CODE

import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;

public class SearchMidlet extends MIDlet implements CommandListener
{
private Display display;
private Form frm1, frm2;
private TextField txtfsearch;
private Command cmdquit;
private Command cmdback;
private Command cmdsearch;
private String strSearch;
private String strFind;
private String strAlert = "The Searched word position is ";
private ChoiceGroup chgsearchcase;
private Alert alert;
private int fromIndex = 0;
private boolean flag = true;

public SearchMidlet()
{

// Indicating the object to be Displayed

display = Display.getDisplay(this);



//initializing Form’s object
frm1 = new Form("MIDP");
frm2 = new Form("Search Utility");


strSearch = "The J2ME Wireless Toolkit is a set of tools that makes it possible to createapplications for mobile phones and other wireless devices. Although it is based on the Mobile Information Device Profile (MIDP) 2.0, the J2ME Wireless Toolkit also supports a handful of optional packages, making it a widely capable development toolkit.";


//Initailizing TextField’s objects.

txtfsearch = new TextField("Search : ", "", 12, TextField.ANY);


//Initailizing save Command button

cmdquit = new Command("Quit", Command.EXIT, 0);
cmdback = new Command("Back", Command.EXIT, 1);
cmdsearch = new Command("Search", Command.SCREEN, 0);
chgsearchcase = new ChoiceGroup
("Search Case :", Choice.EXCLUSIVE);

//Adding Choice Group to form

chgsearchcase.append("Case Sensetive", null);
chgsearchcase.append("Case Insensetive", null);

//Initailizing Alert object

alert = new Alert
("Search Alert", "", null, AlertType.CONFIRMATION);

//Adding TextFeilds and Commands to from

frm1.addCommand(cmdquit);
frm1.addCommand(cmdsearch);
frm1.append(strSearch);
frm2.addCommand(cmdsearch);
frm2.addCommand(cmdback);
frm2.append(txtfsearch);
frm2.append(chgsearchcase);
frm2.append("");
frm2.append("");

alert.addCommand(cmdsearch);
alert.addCommand(cmdback);

// Adding Command Listner

frm1.setCommandListener(this);
frm2.setCommandListener(this);
alert.setCommandListener(this);
}

public void startApp()
{

// Indicating the object to be Displayed

display.setCurrent(frm1);
}

public void pauseApp()
{
}

public void destroyApp(boolean unconditional)
{
}

public void commandAction(Command command, Displayable displayable)
{

if(command == cmdquit)
{

// Indicating the object to be Displayed

destroyApp(false);
notifyDestroyed();
}

else if(command == cmdsearch)
{

// Indicating the object to be Displayed

if(displayable==frm1)
display.setCurrent(frm2);
}





else if(displayable == frm2)
{
strFind = txtfsearch.getString();
searchUtility();
}

else if(displayable == alert)
{
searchUtility();
}
}
else if(command == cmdback)
{
if(displayable == frm2)
{
display.setCurrent(frm1);
}
else if(displayable == alert)
{
display.setCurrent(frm2); strAlert =
""; flag = true;
}
}
}

public void searchUtility()
{

if(chgsearchcase.getSelectedIndex() == 0)
{
if(flag)
{
fromIndex = strSearch.indexOf(strFind);
flag = false;
}
else

fromIndex = strSearch.indexOf
(strFind, fromIndex + strFind.length());

if(fromIndex != -1)
{
strAlert += String.valueOf(fromIndex) + "\n";
alert.setString(strAlert);
alert.setTimeout(Alert.FOREVER);
display.setCurrent(alert);
}
else

frm2.append(strFind + " does not found.");
}

else
{
if(flag)
{
strFind = strFind.toLowerCase();
strSearch = strSearch.toLowerCase();
fromIndex = strSearch.indexOf(strFind);
flag = false;
}
else

fromIndex = strSearch.indexOf
(strFind, fromIndex + strFind.length());

if(fromIndex != -1)
{
strAlert += String.valueOf
(fromIndex) + "\n";
alert.setString(strAlert);
alert.setTimeout(Alert.FOREVER);
display.setCurrent(alert);

}
else

frm2.append(strFind + " does not found.");

}
}
}

No comments:

Post a Comment