Results 1 to 4 of 4
  1. #1
    PepsiColaMola is offline Member
    Join Date
    Apr 2009
    Posts
    2
    Rep Power
    0

    Default Problem extracting items from a string with separators

    Hey guys!

    I wanted to know if you could help me out here. You could say I'm new to java, I haven't used it for some time, so this question is pretty basic, but it's driving me mad.

    I have a String with the format: 'John','Mary','Anne'

    It has a variable length, it could have 3 names on it, 10 or just 1.

    I just want to extract the names, and save them elsewhere, for example in a String array.

    I've tried this:

    Java Code:
    int index = 0;
    int prev = 0;
    int i = 0; 
    String [] frag = null;
    
    do {
         prev = index;
         index = text.indexOf("','", ant);
         if (index != -1) {
              frag [i]= new String(text.substring(prev + 3, index));
              i++;
         }
    } while (index != -1);
    
    //still have to add the last element
    prev = index;
    index = text.indexOf("'", prev);
    frag [i] = new String(text.substring(prev + 3, index));
    But I'm getting an index out of bounds exception, due to the "prev+3" I used.

    Please help! I realize it's a very simple piece of code, that's why it's making me mad I can't get around to code such a simple problem!

    thanks in advance

    PS: can't use StringTokenizer, I'm programming this in j2me. But I posted here because I think it's a basic java question, it has nothing to do with mobile devices or the j2me characteristics.
    Last edited by PepsiColaMola; 04-24-2009 at 08:15 PM.

  2. #2
    emceenugget is offline Senior Member
    Join Date
    Sep 2008
    Posts
    564
    Rep Power
    6

    Default

    you can do something to check if you are currently parsing the last word, such as if the index of the next single quotation is equal to the string's length-1.

    or you can just use the split() method (check out the javadoc for the string class) to get tokens split by "," and then remove the single quotations subsequently when looking at the individual names

  3. #3
    PepsiColaMola is offline Member
    Join Date
    Apr 2009
    Posts
    2
    Rep Power
    0

    Default

    Hi,

    thanks, but I can't find the split method for the String class in j2me's javadoc. I'm beginning to realize maybe I should have posted this question in the j2me forum.

    Could a kind moderator move it there, please? thanks!

  4. #4
    emceenugget is offline Senior Member
    Join Date
    Sep 2008
    Posts
    564
    Rep Power
    6

    Default

    sorry, didnt see at the bottom of your post that you're using j2me and that it has limited support. in that case, i'd recommend my first suggestion. i'm not really smart on parsing, so someone might have a better strategy than me, though.

Similar Threads

  1. SwT Expand items problem in Solaris.
    By Sureshgurram in forum SWT / JFace
    Replies: 0
    Last Post: 01-27-2009, 10:23 AM
  2. extracting text from jpeg
    By Nicholas Jordan in forum Advanced Java
    Replies: 0
    Last Post: 10-05-2008, 11:40 PM
  3. Replies: 4
    Last Post: 07-07-2008, 01:32 PM
  4. Extracting JAR file
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 02-08-2008, 09:17 AM
  5. program help: Extracting words from a string
    By toad in forum New To Java
    Replies: 1
    Last Post: 11-04-2007, 06:39 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •