java - What is the best way to parse sms -


i developing app parsing sms strings want store in sqlite db.

here sample of how sms look. sample airtime payment.

airtime payment made ugx5,000 kkl(0909xxxx). balance ugx10,000. thank using kkl mobilemoney. 

sample mobile money deposit

you have received ugx100,000 09006700. reason:j. balance ugx170,000. 

sample mobile money sending

you have sent ugx10,000 08970000. reason:j. balance ugx120,000. thank using kkl mobilemoney. 

what interested in is: -the amount send/received /paid. -the number received from/sent to/payment made to. - reason -the balance .

so far have tried using split function .

it not consistent though.i using array index of string token on strings index out bound exception yet on strings works.

here code have far...but works not consistently!

if (str.startswith(received))             {                 // mm deposit                 log.e("msg", str);                 string delimeter="[ .]+";                 string[] tokens= str.split(delimeter);                 (int i=0;i < tokens.length;i++)                 {                     log.e("test",tokens[i]);                         }                                            string amount =tokens[3];                     string reason =tokens[6].trim();                                 string number=tokens[5];                     string balance=tokens[11];                                   log.e("amount",amount);                     log.e("reasons",reason);                     log.e("number",number);                     log.e("balance",balance);                     string type="deposit";                     db.addrecord(amount, type, reason, number, balance);                     db.close();             }             else if(str.startswith(send))             {                    // sent sms                 log.e("msg", str);                        string delimeter="[ .]+";             string[] tokens= str.split(delimeter);             (int i=0;i < tokens.length;i++)             {                 log.e("test",tokens[i]);                     }                                    string amount =tokens[3];                 string reason =tokens[6].trim();                                 string number=tokens[5];                 string balance=tokens[11];                                           log.e("amount",amount);                 log.e("reasons",reason);                 log.e("number",number);                 log.e("balance",balance);                                string type="payment";                 db.addrecord(amount, type, reason, number, balance);                 db.close();             }          else if(str.startswith(airtimepayment))         {       // airtime sms             log.e("msg", str);             string delimeter="[ .]+";             string[] tokens= str.split(delimeter);             (int i=0;i < tokens.length;i++)             {                 log.e("test",tokens[i]);                     }                                        string amount =tokens[4];                 string reason =tokens[0]+ " "+ tokens[1];                    string number=tokens[7];                 string balance=tokens[11];                               log.e("amount",amount);                 log.e("reasons",reason);                 log.e("number",number);                 log.e("balance",balance);                 string type="air time ";                 db.addrecord(amount, type, reason, number, balance);                 db.close();         } 

any tips/advice on how best handle parsing?

i beginner java/android programmer!

ronald

here edit have made

private map<string, string> parsesms(string s){     map<string, string> ret = new hashmap<string, string>();     s = s.replace("\n", "");     stringtokenizer t = new stringtokenizer(s, ".");     while (t.hasmoretokens()){         string b = t.nexttoken().trim();         if (b.startswith("you have sent") ||(b.startswith("you have received"))){             string type = gettype(b);             string parsed = parseamount(b);             string number = parsenumber(b);             ret.put("amount", parsed);             ret.put("number", number);             ret.put("type", type);         }else if(b.startswith("your")){//balance             string parsed = parseamount(b);             ret.put("balance", parsed);         }else if (b.startswith("reason")){              ret.put("reason", b.tostring());         }         else if( b.startswith("airtime"))         {               string type = gettype(b);               string parsed = parseamount(b);               string number = parsenumber(b);               ret.put("amount", parsed);               ret.put("number", number);               ret.put("reason", "air time payment");               ret.put("type", type);           }else if(b.startswith("your")){//balance             string parsed = parseamount(b);             ret.put("balance", parsed);          }       }      return ret; } 

it solved problem.

here solution parsing more robustly.

replace pertinent strings constants. using map lot safer string[] when comes looping. can use match regexp.

private void test(){      string sms = "you have sent ugx10,000 08970000.\n" +             "reason:j.\n" +             "your balance ugx120,000.\n" +             "thank using kkl mobilemoney.";      /*string rec = "you have received ugx100,000 09006700. reason:j.\n" +             "your balance ugx170,000.";     string air = "airtime payment made ugx5,000 kkl(0909xxxx).\n" +             "your balance ugx10,000.\n" +             "thank using kkl mobilemoney.";     */     map<string, string> data = parsesms(sms);     savetodb(data);  }   private void savetodb(map<string, string> data){     db.addrecord(data.get("amount"), data.get("type"), data.get("reason"), data.get("number"), data.get("balance"));     db.close(); }  private map<string, string> parsesms(string s){     map<string, string> ret = new hashmap<string, string>();     s = s.replace("\n", "");     stringtokenizer t = new stringtokenizer(s, ".");     while (t.hasmoretokens()){         string b = t.nexttoken().trim();         if (b.startswith("you have sent") ||  (b.startswith("airtime") || (b.startswith("you have received")))){             string type = gettype(b);             string parsed = parseamount(b);             string number = parsenumber(b);             ret.put("amount", parsed);             ret.put("number", number);             ret.put("type", type);         }else if(b.startswith("your")){//balance             string parsed = parseamount(b);             ret.put("balance", parsed);         }else if (b.startswith("reason")){             ret.put("reason", b.tostring());         }     }      return ret; }  private string gettype(string s){     if (s.startswith("you have sent"))//use constants         return "payment";     else if (s.startswith("airtime"))         return "air time";     else if (s.startswith("you have received"))         return "deposit";      return "unknown"; }   private string parsenumber(string s){     string numberfragment = s.substring(s.lastindexof(' '), s.length());//extract number     return numberfragment; }  private string parseamount(string s){     char[] arr = s.tochararray();     stringbuffer sb = new stringbuffer();     boolean parsingnumber = false;     (char c: arr){         if (character.isdigit(c))             parsingnumber = true;         if (character.isletter(c)  && c != ',' )             parsingnumber = false;         if (parsingnumber && c == ' ')//we have reached end of digit series             break; //done         if (parsingnumber)             sb.append(c);     }      return sb.tostring(); } 

Comments

Popular posts from this blog

Android layout hidden on keyboard show -

google app engine - 403 Forbidden POST - Flask WTForms -

c - Why would PK11_GenerateRandom() return an error -8023? -