TurnsCoffeeIntoScripts
TurnsCoffeeIntoScripts

Reputation: 3918

Appending a java String one character at a time

I have a very simple question, in fact I'm a bit frustrated that I can't solve this on my own but here it is:

strBuffer += arg.charAt( i );

With this line I'm trying to parse a value and add it character by character to a new string. I'm doing this to seperate a single long string into an array of smaller string.

Example, this string

-time delayed -run auto -mode go_n_run

would become this array

strBuffer [0] = -time 
strBuffer [1] = delayed 
strBuffer [2] = -run 
strBuffer [3] = auto 
strBuffer [4] = -mode 
strBuffer [5] = go_n_run

So the line of code with the '+=' doesn't work, nothing gets put in my strBuffer. So I've tried something a little more "complex" that I found on a forum :

strBuffer.concat( new String( new char[]{arg.charAt( i )} ) );

But same result, nothing is put in strBuffer,

So, any hints would be appreciated

Thanks

EDIT : Here is the complete method

String[] args = new String[2 * ARG_LIMIT];
        int index = 0;

        for( int i = 0; i < arg.length(); i++ )
        {
            String strBuffer = new String();

            if( arg.charAt( i ) != ' ' )
            {   

                            // The two methods I've tried
                strBuffer.concat( new String( new char[]{arg.charAt( i )} ) );

                strBuffer += arg.charAt( i );

            }
            else if( arg.charAt( i ) == ' ' )
            {
                args[index] = strBuffer;
                index++;
                strBuffer = "";
            }
        }

Upvotes: 0

Views: 2536

Answers (4)

trutheality
trutheality

Reputation: 23465

It looks like you're trying to rewrite String.split()

More specifically, you're doing this the hard way:

String[] args = arg.split(" ",2*ARG_LIMIT);

What you tried didn't work because strBuffer didn't survive to the next iteration of the for loop. This code would have worked:

    String[] args = new String[2 * ARG_LIMIT];

    int index = 0;
    String strBuffer = new String();

    for( int i = 0; i < arg.length(); i++ )
    {

        if( arg.charAt( i ) != ' ' )
        {   

            strBuffer += arg.charAt( i );

        }
        else if( arg.charAt( i ) == ' ' )
        {
            args[index] = strBuffer;
            index++;
            strBuffer = "";
        }
    }

Upvotes: 1

The Tran
The Tran

Reputation: 470

You should use StringTokenizer. Here is the code:

// the first parameter is the string to be parsed, the second parameter is the delimiters
StringTokenizer st = new StringTokenizer("-time delayed -run auto -mode go_n_run", " ");
while (st.hasMoreTokens()) {
    String s = st.nextToken();
    // append s to your strBuffer
    // ...
}

Upvotes: 1

OmniOwl
OmniOwl

Reputation: 5709

Try print out the character itself that you get from that line at the end. If you get no character, then you obviously are not getting what you are looking for the in the argument array.

Upvotes: 0

nullpotent
nullpotent

Reputation: 9260

I will assume that strBuffer is an instance of java's StringBuffer; if so - you should use strBuffer.append().

But there's a way simpler method for doing the thing you want:

String[] strBuff = arg.split(" "); //split by space

Upvotes: 6

Related Questions