10 Replies - 3836 Views - Last Post: 23 January 2013 - 11:58 PM

#1 offside3002  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 20-January 13

J2ME Textfile Database Problem

Posted 21 January 2013 - 09:48 PM

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:
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;
  }




}



Is This A Good Question/Topic? 0
  • +

Replies To: J2ME Textfile Database Problem

#2 farrell2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 797
  • View blog
  • Posts: 2,422
  • Joined: 29-July 11

Re: J2ME Textfile Database Problem

Posted 21 January 2013 - 10:20 PM

Your code is too long go through, but what does sb contain after you read the file with this?

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();



This should copy all of the text into the file. Does it?
Was This Post Helpful? 1
  • +
  • -

#3 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10175
  • View blog
  • Posts: 37,578
  • Joined: 27-December 08

Re: J2ME Textfile Database Problem

Posted 21 January 2013 - 10:27 PM

Moved to Mobile Development.
Was This Post Helpful? 0
  • +
  • -

#4 offside3002  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 20-January 13

Re: J2ME Textfile Database Problem

Posted 21 January 2013 - 10:34 PM

I'm sorry sir but I copied the wrong code.
Here is the working code sir for the correcting process:
         data = file();
          text = message.getString();
          StringTokenizer stText = new StringTokenizer(text," ");

          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 = "";
          message = new TextBox ("Corrected Message", temp, 2000, TextField.ANY);
          message.addCommand(translate);
          message.addCommand(send);
          message.setCommandListener(this);
          display.setCurrent(message);

      }

and the code which will look a word in the text file:
  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;
  }


Yes sir I think sb contained the file from the database...
Was This Post Helpful? 0
  • +
  • -

#5 offside3002  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 20-January 13

Re: J2ME Textfile Database Problem

Posted 21 January 2013 - 10:39 PM

View Postfarrell2k, on 21 January 2013 - 10:20 PM, said:

Your code is too long go through, but what does sb contain after you read the file with this?

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();



This should copy all of the text into the file. Does it?


Yes sir I think so..
Was This Post Helpful? 0
  • +
  • -

#6 farrell2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 797
  • View blog
  • Posts: 2,422
  • Joined: 29-July 11

Re: J2ME Textfile Database Problem

Posted 21 January 2013 - 11:01 PM

Can you clarify your problem? You have me confused. Please be more specific.
Was This Post Helpful? 0
  • +
  • -

#7 offside3002  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 20-January 13

Re: J2ME Textfile Database Problem

Posted 21 January 2013 - 11:23 PM

View Postfarrell2k, on 21 January 2013 - 11:01 PM, said:

Can you clarify your problem? You have me confused. Please be more specific.


Sir I want my code to read a phrase in the text file not just a word. And I don't know how to do it.

here is the code sir that will get the data in the textbox and compare it with the data in the database:
  data = file2();
          text = message.getString();
          StringTokenizer stText = new StringTokenizer(text," ");

          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 + " ";
              }

And the code that will look in the database:
 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;
  }

Was This Post Helpful? 0
  • +
  • -

#8 farrell2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 797
  • View blog
  • Posts: 2,422
  • Joined: 29-July 11

Re: J2ME Textfile Database Problem

Posted 22 January 2013 - 06:56 AM

Then don't break it up into single words with this:

StringTokenizer stText = new StringTokenizer(text," ");



This line here separates every your file into single words, so a text file with the data: "How are you | I am OK" would be tokenized as such:

How
are
you
|
I
am
OK
Was This Post Helpful? 1
  • +
  • -

#9 offside3002  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 20-January 13

Re: J2ME Textfile Database Problem

Posted 23 January 2013 - 05:35 AM

View Postfarrell2k, on 22 January 2013 - 06:56 AM, said:

Then don't break it up into single words with this:

StringTokenizer stText = new StringTokenizer(text," ");



This line here separates every your file into single words, so a text file with the data: "How are you | I am OK" would be tokenized as such:

How
are
you
|
I
am
OK


So what code should I replace with this sir?
StringTokenizer stText = new StringTokenizer(text," ");

Was This Post Helpful? 0
  • +
  • -

#10 farrell2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 797
  • View blog
  • Posts: 2,422
  • Joined: 29-July 11

Re: J2ME Textfile Database Problem

Posted 23 January 2013 - 07:46 AM

Nothing. It splits your tokens into single words. You said that you did not want that.
Was This Post Helpful? 0
  • +
  • -

#11 offside3002  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 20-January 13

Re: J2ME Textfile Database Problem

Posted 23 January 2013 - 11:58 PM

View Postfarrell2k, on 23 January 2013 - 07:46 AM, said:

Nothing. It splits your tokens into single words. You said that you did not want that.

Yeah but i want it to read a phrase not just a single word. What should i do?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1