954,529 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

How to split StringBuffer in J2ME

Hello all,
I am retrieving data from servlet to midlet. Now coming data is whole string. but now I want to chop it into different words. here my code is :

HttpConnection conn = (HttpConnection) Connector.open(url);
               StringBuffer sb = new StringBuffer();
               conn.setRequestMethod(HttpConnection.GET);
               conn.setRequestProperty("Content-Type", "Application/x-urlformencoded");
               InputStream is = conn.openInputStream();
               byte[] b = new byte[(int) conn.getLength()];
               int ch = 0;
               while ((ch = is.read()) != -1) {
                  sb.append((char) ch);
               }


               System.out.println(sb);



and the output is

LG

WIPRO

VIDEOCON

DELL

HP

COMPAQ

But now I want each word and append them into list. What can I do?

Hakoo
Junior Poster
117 posts since Mar 2010
Reputation Points: 24
Solved Threads: 5
 

This page from rim gives examples of string utility functions for j2me, the first on the page (split) should do the trick

burgercho
Light Poster
47 posts since Jul 2008
Reputation Points: 26
Solved Threads: 11
 

Thanks burgercho. Its really Helpful. Even I found one code.

public static String[] stringToArray(String a, String delimeter) {
      String c[] = new String[0];
      String b = a;
      while (true) {
         int i = b.indexOf(delimeter);
         String d = b;
         if (i >= 0) {
            d = b.substring(0, i);
         }
         String e[] = new String[c.length + 1];
         for (int k = 0; k < c.length; k++) {
            e[k] = c[k];
         }
         e[e.length - 1] = d;
         c = e;
         b = b.substring(i + delimeter.length(), b.length());
         if (b.length() <= 0 || i < 0) {
            break;
         }
      }
      return c;
   }

This is also working.

Hakoo
Junior Poster
117 posts since Mar 2010
Reputation Points: 24
Solved Threads: 5
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: