String.toCharArray() problemo

Hello, I am trying to convert a String to an array of char's so that I can manipulate each char without using String.charAt for every single character. My problem is that the toCharArray() method doesn't seem to be functioning properly.

For example,

String input ="BOBOBOBOBO";

char inArray[] = input.toCharArray();

I get this as my output:

INPUT: BOBOBOBOBO

INARRAY: [C@89ae9e

any ideas? Thanks,

dub

[517 byte] By [dub_styleea] at [2007-11-27 11:50:51]
# 1

Printing a char[] doesn't print each individual character like you think it does. Call print on each element of the array instead.

hunter9000a at 2007-7-29 18:34:28 > top of Java-index,Java Essentials,New To Java...
# 2

what do you expect? when you print an array, it is an object, and the toString() prints out a textual representation of the array. you either need to loop thru the array to print it out, or use the Arrays.toString(char[]) method available since 1.5 to print it out as a string.

~Tim

SomeoneElsea at 2007-7-29 18:34:28 > top of Java-index,Java Essentials,New To Java...
# 3

I was calling inArray.toString(); ... that doesn't print the array as a string?

dub_styleea at 2007-7-29 18:34:28 > top of Java-index,Java Essentials,New To Java...
# 4

I had no trouble with the code snippet. What else is there to your program?

Brightness86a at 2007-7-29 18:34:28 > top of Java-index,Java Essentials,New To Java...
# 5

Here is the entire method.. what it is supposed to do is replace color codes which I define with the ANSI escape codes to the corresponding colors.

public static String parseColor(String input)

{

char inArray[] = input.toCharArray();

char outArray[] = {' '};

String output;

int j = 0, k = 0, p = 0;

int c = 0, max;

System.out.println("INPUT: " + input);

System.out.println("INARRAY: " + Arrays.toString(inArray));

System.out.println("INPUT.TOCHARARRAY: " + input.toCharArray().toString());

if (inArray[0] == '\0')

return input;

while (inArray[0] != '\0')

{

if (inArray[j] == '&' && (getColor(inArray[j + 1]) != -1)) {

c = getColor(inArray[j + 1]);

j += 2;

} else {

outArray[p] = inArray[j];

j++;

p++;

continue;

}

if (c > MAX_COLORS)

c = 0;

max = COLOR_LIST[c].length();

if (max == 1)

for (k = 0; k < max; k++) {

outArray[p] = COLOR_LIST[c].charAt(k);

p++;

}

}

outArray[p] = '\0';

output = outArray.toString();

System.out.println("OUTARRAY: " + outArray.toString());

System.out.println("OUTPUT: " + output);

return output;

}

dub_styleea at 2007-7-29 18:34:28 > top of Java-index,Java Essentials,New To Java...
# 6

try

String text = "bobobo";

char[] index = text.toCharArray();

for(int x = 0; x <= 5; x++)

{

System.out.println(index[x]);

}

opps forgot to put in the X :)

Message was edited by:

mark07

mark07a at 2007-7-29 18:34:28 > top of Java-index,Java Essentials,New To Java...
# 7

And that should be "Bobobo-bo Bo-bobo"

http://en.wikipedia.org/wiki/Bobobo-bo_Bo-bobo

BigDaddyLoveHandlesa at 2007-7-29 18:34:28 > top of Java-index,Java Essentials,New To Java...
# 8

> And that should be "Bobobo-bo Bo-bobo"

>

> http://en.wikipedia.org/wiki/Bobobo-bo_Bo-bobo

also known as Bo-bobo

mark07a at 2007-7-29 18:34:28 > top of Java-index,Java Essentials,New To Java...
# 9

> > And that should be "Bobobo-bo Bo-bobo"

> >

> > http://en.wikipedia.org/wiki/Bobobo-bo_Bo-bobo

>

> also known as Bo-bobo

... to his buds.

BigDaddyLoveHandlesa at 2007-7-29 18:34:28 > top of Java-index,Java Essentials,New To Java...
# 10

Ok, using a for() loop to print the array works fine.. so this will be how I copy the outArray to the output String?

for (index = 0; index < outArray.length(); index++)

output.append(outArray[index])

dub_styleea at 2007-7-29 18:34:28 > top of Java-index,Java Essentials,New To Java...
# 11

output = new String(outArray);

~Tim

SomeoneElsea at 2007-7-29 18:34:28 > top of Java-index,Java Essentials,New To Java...
# 12

Okay, now that we got that array stuff out of the way.. I am running into another problem.

I have a few color codes, like I said:

final static String CBLU = "\033[0;34m";

.......

and then an array of Strings called COLOR_LIST which contains each of these color codes. I get to the following code and it hangs up:

max = COLOR_LIST[c].length();

for (k = 0; k < max; k++) {

System.out.println("Inside for loop: k = " + k);

outArray[p] = COLOR_LIST[c].charAt(k);

p++;

}

when I print max, it = 7.. for some reason the for loop stops at k = 1... any ideas?

dub_styleea at 2007-7-29 18:34:28 > top of Java-index,Java Essentials,New To Java...
# 13

<max = COLOR_LIST[c].length();

I'm pretty sure that takes the length of the index of coler_list at C which would be 1 becuase it contains 1 string when you put values into the color list add a simple addition that is max++; to keep track of how many you have>

mark07a at 2007-7-29 18:34:28 > top of Java-index,Java Essentials,New To Java...
# 14

I tried to print max and it said max = 7. The ANSI sequence is 7 if you count \033 as 1 char.

dub_styleea at 2007-7-29 18:34:28 > top of Java-index,Java Essentials,New To Java...
# 15

print out max inside your for loop... see what its doing

mark07a at 2007-7-29 18:34:33 > top of Java-index,Java Essentials,New To Java...
# 16

Ok, I changed it to:

max = COLOR_LIST[c].length();

for (k = 0; k < max; k++) {

System.out.println("Inside for loop: k = " + k + ", max = " + max);

System.out.print(COLOR_LIST[c].charAt(k));

outArray[p] = COLOR_LIST[c].charAt(k);

p++;

}

System.out.println("Passed for() loop.");

and I get the following output:

Inside for loop: k = 0, max = 7

Inside for loop: k = 1, max = 7

[

It's getting hung up when it tries to add the [ to the outArray apparently.. I don't understand what could be the problem?!?!? :P

dub_styleea at 2007-7-29 18:34:33 > top of Java-index,Java Essentials,New To Java...
# 17

ok well i don't know the code that's before it so ill just say what i see

1) is p initialized and set to 0 or w/e you want?

2) do you always can't c to be constant? becuase its not changing

3) is your outArray[] set dimensions ok?

4) does it get stuck or does it exit or error out of the for loop?

mark07a at 2007-7-29 18:34:33 > top of Java-index,Java Essentials,New To Java...
# 18

> ok well i don't know the code that's before it so ill

> just say what i see

>

> 1) is p initialized and set to 0 or w/e you want?

Yes, p is initialized to 0.

> 2) do you always can't c to be constant? becuase its

> not changing

c = the index of the color in the COLOR_LIST

> 3) is your outArray[] set dimensions ok?

This may be where my problem is. I had it defined like so:

char outArray[] = {' '};

So I would guess that when trying to assign a value to outArray[1] it may run into some problems!! But for some reason, it's not throwing any exceptions or errors. It will just stop when k = 1.

How will I define a new array to be big enough to handle the inArray + any additional color codes that are parsed? Should I just max it out and waste all the extra space every time it calls the method?

> 4) does it get stuck or does it exit or error out of

> the for loop?

See above.

dub_styleea at 2007-7-29 18:34:33 > top of Java-index,Java Essentials,New To Java...
# 19

> ok well i don't know the code that's before it so ill

> just say what i see

>

> 1) is p initialized and set to 0 or w/e you want?

Yes, p is initialized to 0.

> 2) do you always can't c to be constant? becuase its

> not changing

c = the index of the color in the COLOR_LIST

> 3) is your outArray[] set dimensions ok?

This may be where my problem is. I had it defined like so:

char outArray[] = {' '};

So I would guess that when trying to assign a value to outArray[1] it may run into some problems!! But for some reason, it's not throwing any exceptions or errors. It will just stop when k = 1.

How will I define a new array to be big enough to handle the inArray + any additional color codes that are parsed? Should I just max it out and waste all the extra space every time it calls the method?

> 4) does it get stuck or does it exit or error out of

> the for loop?

See above.

dub_styleea at 2007-7-29 18:34:33 > top of Java-index,Java Essentials,New To Java...
# 20

Ok, I fixed it by making outArray larger. I also learned that Java doesn't use the '\0' string terminator that C uses. The darn thing was getting stuck in the middle of a while() loop. Thanks for all your help.

dub_styleea at 2007-7-29 18:34:33 > top of Java-index,Java Essentials,New To Java...
# 21

ya no problem glad you figured it out

mark07a at 2007-7-29 18:34:33 > top of Java-index,Java Essentials,New To Java...