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
Printing a char[] doesn't print each individual character like you think it does. Call print on each element of the array instead.
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
I was calling inArray.toString(); ... that doesn't print the array as a string?
I had no trouble with the code snippet. What else is there to your program?
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;
}
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
And that should be "Bobobo-bo Bo-bobo"
http://en.wikipedia.org/wiki/Bobobo-bo_Bo-bobo
> And that should be "Bobobo-bo Bo-bobo"
>
> http://en.wikipedia.org/wiki/Bobobo-bo_Bo-bobo
also known as Bo-bobo
> > 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.
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])
output = new String(outArray);
~Tim
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?
<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>
I tried to print max and it said max = 7. The ANSI sequence is 7 if you count \033 as 1 char.
print out max inside your for loop... see what its doing
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
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?
> 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.
> 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.
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.
ya no problem glad you figured it out