0

Hi my problem is that i'm making a mobile app and i need to seperate the following string:

ChrisHunter21

into:

Chris
Hunter
21

What is the easiest way to do this in J2ME ?

i had a look at the Ostermiller one but didnt understand it.

Thanks Chris

3
Contributors
5
Replies
6
Views
7 Years
Discussion Span
Last Post by peter_budo
0

How is that delimited? Is there anything between the sH or the r2?
Suppose the name is
HamishMacDonald64
or
Jean-ClaudeVanDamme75
?

0

i've never used a tokeniser before so i dont know what delimited is. However there is a space between s and H and also between r and 2.

I get the string from a record store and need to tokenise it after its been retreived.

sorry if my description is a little vague i dont have much experience with record stores or tokenising

thanks

Chris

0

OK, it looked like no spaces. With spaces its easy.
This is from the StringTokeniser documentation:

The following is one example of the use of the tokenizer. The code:

     StringTokenizer st = new StringTokenizer("this is a test");
     while (st.hasMoreTokens()) {
         System.out.println(st.nextToken());
     }

prints the following output:

     this
     is
     a
     test

end quote.

Assuming its the same in Java ME, that's pretty much all you need to know!

Uh-oh. Looks like StringTokenizer is deprecated in ME. The String class has a split method that splits things up OK. Look at that.

Edited by mike_2000_17: Fixed formatting

0

Thanks for you help, i figured it out and used the split method heres my code:

private void showAllRecords()
    {
        //gets entered ID 
        int recID = Integer.parseInt(textField6.getString());
        try
        {
            byte[] Details = new byte[frs.getRecordSize(recID)];
            // Gets record using entered ID
            Details = frs.getRecord(recID);
            // Calls split() and stores vextor returned by split()
            String[] splitStr = split( new String (Details));
            // Uses array index to store appropreate array value in textField
            textField3.setString(splitStr[0]);
            textField4.setString(splitStr[1]);
            textField5.setString(splitStr[2]);                
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }
    

    // Split a string with symblo, return vecotr
    private String[] split(String original)
    {
        Vector nodes = new Vector();
        String separator = ",";
        // Parse nodes into vector
        int index = original.indexOf(separator);
        while(index>=0)
        {
            nodes.addElement( original.substring(0, index) );
            original = original.substring(index+separator.length());
            index = original.indexOf(separator);
        }
        // Get the last node
        nodes.addElement( original );
        // Create string array with segments of split string
        result = new String[nodes.size()];
        if( nodes.size()>0 )
        {
            for(int loop=0; loop<nodes.size(); loop++)
            {
            result[loop] = (String)nodes.elementAt(loop);
            }
        }
    return result;
    }

Some of the comments may be wrong but code works.

0

1. Post moved to Mobile development section
2. James not sure about deprecation of Tokenizer, as far I know it was never part of Microedition
3. Chris I think your problem is in the way of storing data in RecordStore. Since you are transforming your entries directly to byte array you have to go through string splitting. If you used DataInputStream and DataOutputStream you would make your life easier. You can use read/write in type oriented manner meaning use readBoolean(), readDouble(), writeFloat() writeUTF() etc

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.