Hello Everyone,
I am developing a mobile application using J2ME, my problem is my codes doesn't read a word in the text file which has a space. In my database which I used a text file the words looks like this: hw r u?|how are you?|.I used this symbol "|" as a delimiter. But my code will read if it is only a single word without any space. Please anyone help me...Thank you in advance..
My code is this:
I am developing a mobile application using J2ME, my problem is my codes doesn't read a word in the text file which has a space. In my database which I used a text file the words looks like this: hw r u?|how are you?|.I used this symbol "|" as a delimiter. But my code will read if it is only a single word without any space. Please anyone help me...Thank you in advance..
My code is this:
import java.io.InputStream;
import javax.microedition.io.*;
import javax.microedition.lcdui.*;
import javax.microedition.midlet.MIDlet;
import javax.wireless.messaging.*;
import net.mypapit.java.StringTokenizer;
public class CELTACS extends MIDlet implements CommandListener {
Display display;
private String enterword,transword,dbword;
private String data,text,temp,finalmessage,finalmess,messages,mess;
private String newtxt = "";
private TextBox message,toWhom;
private Alert alert;
private Command send,exit,celtacs,translate,ok,back;
MessageConnection clientConn;
public Ticker ticker = new Ticker(
"Cebuano/English Language Translator and Corrector for SMS");
public CELTACS() {
display=Display.getDisplay(this);
toWhom=new TextBox("Enter Phone Number","",50,TextField.PHONENUMBER);
message=new TextBox("Message","",600,TextField.ANY);
celtacs=new Command ("Correct TextSpeak",Command.ITEM,0);
translate=new Command ("Translate",Command.ITEM,0);
back=new Command ("Back",Command.BACK,2);
ok=new Command("Ok",Command.ITEM,0);
send=new Command("Send",Command.BACK,0);
exit=new Command("Exit",Command.SCREEN,5);
message.setTicker(ticker);
message.addCommand(send);
message.addCommand(celtacs);
message.addCommand(exit);
toWhom.addCommand(ok);
message.setCommandListener(this);
}
public void startApp() {
display.setCurrent(message);
}
public void pauseApp() {
}
public void destroyApp(boolean unconditional) {
notifyDestroyed();
}
public static String capitalize(String str)
{
char[]arr=str.toCharArray();
boolean cap=true;
boolean spacefound=true;
for (int i=0;i<arr.length;i++)
{
if(cap)
{
if(arr[i]== ' ')
{
spacefound=true;
}
else
{
if(spacefound && !Character.isUpperCase(arr[i]))
{
arr[i]=Character.toUpperCase(arr[i]);
}
cap=false;
spacefound=false;
}
}
else
{
if(arr[i] =='.' ||arr[i] =='?' ||arr[i] =='!')
{
cap=true;
spacefound=true;
}
}
}
return new String(arr);
}
public static String addSpaces(String str)
{
char[]arr = str.toCharArray();
StringBuffer result = new StringBuffer();
boolean markSpecialChar = false;
for(int i=0; i<arr.length;i++)
{
if(markSpecialChar)
{
if(arr[i] !=' ')
{
result.append(' ');
}
markSpecialChar=false;
}
result.append(arr[i]);
if(arr[i] == '.' || arr[i] == '?' || arr[i] == '!')
{
markSpecialChar = true;
}
}
return result.toString();
}
public void commandAction(Command cmd,Displayable disp) {
if(cmd==exit) {
destroyApp(false);
}
if(cmd==send){
display=Display.getDisplay(this);
display.setCurrent(toWhom);
toWhom.setTicker(ticker);
toWhom.setCommandListener(this);
}
if(cmd==ok)
{
String mno=toWhom.getString();
String msg=message.getString();
if(mno.equals("")) {
alert = new Alert("");
alert.setString("Enter Mobile Number!!!");
alert.setTimeout(2000);
display.setCurrent(alert);
}
else {
try {
clientConn=(MessageConnection)Connector.open("sms://"+mno);
}
catch(Exception e) {
alert = new Alert("Attention!!");
alert.setString("You are unable to send your message because of network problem.");
alert.setTimeout(2000);
display.setCurrent(alert);
}
try {
TextMessage textmessage = (TextMessage) clientConn.newMessage(MessageConnection.TEXT_MESSAGE);
textmessage.setAddress("sms://"+mno);
textmessage.setPayloadText(msg);
clientConn.send(textmessage);
}
catch(Exception e)
{
Alert alert=new Alert("Alert","",null,AlertType.INFO);
alert.setTimeout(Alert.FOREVER);
alert.setString("Unable to send");
display.setCurrent(alert);
}
}
}
if(cmd == celtacs)
{
String msg = message.getString();
if(msg.equals(""))
{
alert = new Alert("Please");
alert.setString("Enter Your Message!!!");
alert.setTimeout(2000);
display.setCurrent(alert,message);
}
else
if(cmd == translate)
data = file();
text = message.getString();
finalmessage=addSpaces(text);
StringTokenizer stText = new StringTokenizer(finalmessage," ");
while(stText.hasMoreTokens())
{
int found = 0;
dbword = stText.nextToken();
String str = dbword;
StringTokenizer st = new StringTokenizer(data,"|");
while ((st.hasMoreTokens()) && (found==0))
{
enterword = st.nextToken();
transword = st.nextToken();
if(enterword.equalsIgnoreCase(dbword))
{
found = 1;
}
}
if(found==1)
{
newtxt = newtxt + "" + transword + " ";
}
else
{
newtxt = newtxt + " " + dbword + " ";
alert = new Alert("Please");
alert.setString("Please check the spelling or");
alert.setString("the word was not found in the database");
alert.setTimeout(5000);
display.setCurrent(alert);
}
}
if(text.equals(""))
{
newtxt = "";
}
temp = newtxt;
newtxt = "";
finalmess=capitalize(temp);
message = new TextBox ("Corrected Message", finalmess, 2000, TextField.ANY);
message.addCommand(translate);
message.addCommand(back);
message.addCommand(send);
message.addCommand(exit);
message.setCommandListener(this);
display.setCurrent(message);
}
{
data = file2();
text = message.getString();
messages=addSpaces(text);
StringTokenizer stText = new StringTokenizer(messages," ");
while(stText.hasMoreTokens())
{
int found = 0;
dbword = stText.nextToken();
StringTokenizer st = new StringTokenizer(data,"\\|");
while ((st.hasMoreTokens()) && (found==0))
{
enterword = st.nextToken();
transword = st.nextToken();
if(enterword.equalsIgnoreCase(dbword))
{
found = 1;
}
}
if(found==1)
{
newtxt = newtxt + "" + transword + " ";
}
else
{
newtxt = newtxt + "" + dbword + " ";
alert = new Alert("Please");
alert.setString("Please check the spelling or");
alert.setString("the word was not found in the database");
alert.setTimeout(5000);
display.setCurrent(alert);
}
}
if(text.equals(""))
{
newtxt = "";
}
temp = newtxt;
newtxt = "";
mess=capitalize(temp);
message = new TextBox ("Translated Message", mess, 2000, TextField.ANY);
message.addCommand(send);
message.setCommandListener(this);
display.setCurrent(message);
if(cmd == exit)
{
display.setCurrent(message);
}
}
}
private String file()
{
InputStream is = getClass().getResourceAsStream("textspeak.txt");
StringBuffer sb = new StringBuffer();
try{
int chars;
while ((chars = is.read()) != -1)
{
sb.append((char) chars);
}
return sb.toString();
}catch (Exception e){}
return null;
}
private String file1()
{
InputStream is = getClass().getResourceAsStream("English.txt");
StringBuffer sb = new StringBuffer();
try{
int chars;
while ((chars = is.read()) != -1)
{
sb.append((char) chars);
}
return sb.toString();
}catch (Exception e){}
return null;
}
private String file2()
{
InputStream is = getClass().getResourceAsStream("translate.txt");
StringBuffer sb = new StringBuffer();
try{
int chars;
while ((chars = is.read()) != -1)
{
sb.append((char) chars);
}
return sb.toString();
}catch (Exception e){}
return null;
}
}