|
![]() |
Overclock.net - Overclocking.net > Software, Programming and Coding > Coding and Programming > Application Programming | |
Java char[] to string?
|
||
![]() |
|
|
LinkBack | Thread Tools |
|
|
#1 (permalink) | |||||||||||||
|
Intel Overclocker
|
Hey guys,
I'm writing a method for an assignment for school and it needs to take an inputted char array and convert it into a string. Here is what I have but it doesn't execute. It compiles fine but gets a runtime error: java.lang.ArrayIndexOutOfBoundsException: 5 at Tester.charArrayToString(Tester.java:43) So really my question is this, how do I convert a char to a string so that I can add it together in the for loop? The method is listed below. Thanks a billion in advance! (not sure if i have to put this in a quote or code block or something but here it is) public String charArrayToString(char[] c) { String sS = ""; for(int b=0; b<=c.length; b++) { sS = sS + c ; } return sS; }
__________________
3DMARK 06 SCORE: TBA
|
|||||||||||||
|
|
|
|
|
#2 (permalink) | |||||||||||||
|
Intel Overclocker
|
bump! it's pretty urgent lol
__________________
3DMARK 06 SCORE: TBA
|
|||||||||||||
|
|
|
|
|
#3 (permalink) | |||||||||||||
|
Intel Overclocker
|
completely shameless bump to keep this on the top of the homepage!
__________________
3DMARK 06 SCORE: TBA
|
|||||||||||||
|
|
|
|
|
#4 (permalink) | |||||||||||||
|
Kernel Sanders
|
can you do this?
String s = new String(c); **EDIT** Yup. For future reference check the Java API. It's a fantastic resource The error in the code was that the limit should be b < c.length, not b <= c.length. Array indices go from 0 -> length-1, not 1->length
Last edited by rabidgnome229 : 06-12-08 at 08:05 PM. |
|||||||||||||
|
|
|
|
#5 (permalink) | ||||||||||||
|
Overclocker
|
I'll check it out and try to get back to you....
EDIT: BTW stop bumping.
__________________
Fold For The Cause ! Fold For The People .
|
||||||||||||
|
|
|
|
|
#6 (permalink) | ||||||||||||||
|
Intel Overclocker
|
Quote:
EDIT: All I had to do was change that condition like you said. I'm a dummy, I knew that I just spaced out lol. Thanks again! It works now, without the need for that line. I think he wanted us to use a for loop but i'm going to try it as well with that String s = new String(c). lol sorry, i know i shouldn't do that, i was just kind of impatient. childish i know
__________________
3DMARK 06 SCORE: TBA
Last edited by GANDALFtheGREY : 06-12-08 at 08:12 PM. |
||||||||||||||
|
|
|
|
|
#7 (permalink) | |||||||||||||||
|
Linux Lobbyist
|
Quote:
![]()
__________________
Quote:
(450x8) (417x9) (425x9)![]()
|
|||||||||||||||
|
|
|
|
#8 (permalink) | |||||||||||||
|
Kernel Sanders
|
Creating an empty string and iteratively concatenating a single character will be painfully slow. Use java's constructor - it will be optimized.
|
|||||||||||||
|
|
|
|
#9 (permalink) | |||||||||||||
|
Intel Overclocker
|
Yeah, I ended up doing that, lol, 2 line method rather than a 6 line method, woot! I hope my prof accepts it, I included the original way I did it in a comment anyway so he can check it out. Thanks again! I didn't even know that Java API existed! This will definitely save me in future snags.
__________________
3DMARK 06 SCORE: TBA
|
|||||||||||||
|
|
|
|
|
#10 (permalink) | ||||||||||||||
|
Fear is the heart of Love
![]() |
Yeah, The java site and API are the best places to find something if you need something or forget what a certain method does, etc.
__________________
"UNIX was never designed to keep people from doing stupid things, because that policy would also keep them from doing clever things." - Doug Gwyn Try out the latest Programming Challenge Quote:
CPU-Z Validation @ 2.97-prime95 stable 16 hours @ 1.48v Proof | CPU-Z Validation @ 3.15 Getting Mouse Side Buttons to work in Linux, Compile a custom Kernel, More
|
||||||||||||||
|
|
![]() |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
|
|