PDA

View Full Version : Java character arrays...


Nebula
03-18-2003, 06:07 PM
</span><table border="0" align="center" width="95%" cellpadding="0" cellspacing="0"><tr><td>Code Sample </td></tr><tr><td id="CODE">private String arr2str(char[] chars, int charsRead)
{
Â* Â* Â* Â*String buffer = "";
Â* Â* Â* Â*for (int i = 0; i < charsRead; i++)
Â* Â* Â* Â*{
Â* Â* Â* Â* Â* Â* Â* Â*System.out.println("Loading "+i+"...");
Â* Â* Â* Â* Â* Â* Â* Â*buffer += chars[i];
Â* Â* Â* Â*}
Â* Â* Â* Â*return buffer;
}[/QUOTE]<span id='postcolor'>

I'm trying to convert a character array into a String in Java. Â*The FileReader reads the file into an array of characters. Â*Using the method above to convert the array into a string is painfully slow.

Is there a built-in way for just turning a charater array into a string? Â*using .toString() on the array produces something like: &quot;[C@e51b2c&quot;. Â*I hope this isn't *too* easy of a question.

BTW...only reason I'm not going to some source code website and looking this up is because this is part of my Adv. Java program, and I'm only supposed to use JavaDocs or TheJavaTutorial (and if you use that reason for not telling me, I'll just submit a program that takes forever to load big files http://campcaen.engin.umich.edu/iB_html/non-cgi/emoticons/biggrin.gif). Â*The code I *did* submit is so obvious...it's nothing special. Â*If a councelor doesn't like that I posted it, by all means, censor it out. And odds are, if they understand it, they'd have already coded it, or know how to, and if they don't understand it, they won't use it.

Nebula
03-18-2003, 06:27 PM
bah...I hate it when I answer my own question.

I just changed the amount of data I loaded at one time. I was having that function parse the file all at once, but it's much faster to take it in smaller chunks http://campcaen.engin.umich.edu/iB_html/non-cgi/emoticons/laugh.gif.

jyorke
03-29-2003, 01:39 AM
It's really quite simple to convert a char array into a string in Java... The String class has a constructor and a static method for that very purpose. An equivilent version of that code would be:

</span><table border="0" align="center" width="95%" cellpadding="0" cellspacing="0"><tr><td>Code Sample </td></tr><tr><td id="CODE">private String arr2str(char[] chars)
{
String retval = new String(chars);
return retval;
}[/QUOTE]<span id='postcolor'>
Or inline:
</span><table border="0" align="center" width="95%" cellpadding="0" cellspacing="0"><tr><td>Code Sample </td></tr><tr><td id="CODE">String.valueOf(chars)[/QUOTE]<span id='postcolor'>
The API is your friend. See http://java.sun.com/j2se/1.4.1/docs/api/java/lang/String.html

Also, I never said that you can't look at other code for inspiration when doing the advanced project, just that all code that you submit must be your own. The tutorial and API links were just helpful resources...

Nebula
03-29-2003, 11:50 AM
heh, cool. Thanks, I knew there had to be a better way. And yes, I do use the API documentation a lot, I just never thought to use it for this one.

Thankies http://campcaen.engin.umich.edu/iB_html/non-cgi/emoticons/wink.gif