New Posts  All Forums:Forum Nav:

Java char[] to string?

post #1 of 17
Thread Starter 
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;
}





post #2 of 17
Thread Starter 
bump! it's pretty urgent lol
post #3 of 17
Thread Starter 
completely shameless bump to keep this on the top of the homepage!
post #4 of 17
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
Edited by rabidgnome229 - 6/12/08 at 5:05pm
It goes to eleven
(13 items)
 
  
CPUMotherboardGraphicsRAM
E6300 DS3 EVGA 8600GTS 2GB XMS2 DDR2-800 
Hard DriveOSMonitorKeyboard
1.294 TB Arch Linux/XP Samsung 226bw Eclipse II 
PowerCaseMouse
Corsair 520HX Lian-Li v1000B Plus G7 
  hide details  
Reply
It goes to eleven
(13 items)
 
  
CPUMotherboardGraphicsRAM
E6300 DS3 EVGA 8600GTS 2GB XMS2 DDR2-800 
Hard DriveOSMonitorKeyboard
1.294 TB Arch Linux/XP Samsung 226bw Eclipse II 
PowerCaseMouse
Corsair 520HX Lian-Li v1000B Plus G7 
  hide details  
Reply
post #5 of 17
I'll check it out and try to get back to you....

EDIT: BTW stop bumping.
post #6 of 17
Thread Starter 
Quote:
Originally Posted by rabidgnome229 View Post
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
Thank you very much! I'll try that.

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).

Quote:
Originally Posted by voice View Post
I'll check it out and try to get back to you....

EDIT: BTW stop bumping.
lol sorry, i know i shouldn't do that, i was just kind of impatient. childish i know
Edited by GANDALFtheGREY - 6/12/08 at 5:12pm
post #7 of 17
Quote:
Originally Posted by rabidgnome229 View Post
For future reference check the Java API. It's a fantastic resource
lifesaver of a link
Jesus PC v5.3
(13 items)
 
  
CPUMotherboardGraphicsRAM
i7-950 Gigabyte X58A-UD3R 5850 GSkill 1600 MHz 4x4GB 
Hard DriveOptical DriveOSMonitor
3x1200.12 500GB RAID 0, 2x2TB Deskstar RAID 1 Samsung Writemaster SATA Gentoo, Windows 7 x64 3x 21.5" Samsungs 
PowerCaseMouse
Thermaltake 750W RocketPwn G500 
  hide details  
Reply
Jesus PC v5.3
(13 items)
 
  
CPUMotherboardGraphicsRAM
i7-950 Gigabyte X58A-UD3R 5850 GSkill 1600 MHz 4x4GB 
Hard DriveOptical DriveOSMonitor
3x1200.12 500GB RAID 0, 2x2TB Deskstar RAID 1 Samsung Writemaster SATA Gentoo, Windows 7 x64 3x 21.5" Samsungs 
PowerCaseMouse
Thermaltake 750W RocketPwn G500 
  hide details  
Reply
post #8 of 17
Creating an empty string and iteratively concatenating a single character will be painfully slow. Use java's constructor - it will be optimized.
It goes to eleven
(13 items)
 
  
CPUMotherboardGraphicsRAM
E6300 DS3 EVGA 8600GTS 2GB XMS2 DDR2-800 
Hard DriveOSMonitorKeyboard
1.294 TB Arch Linux/XP Samsung 226bw Eclipse II 
PowerCaseMouse
Corsair 520HX Lian-Li v1000B Plus G7 
  hide details  
Reply
It goes to eleven
(13 items)
 
  
CPUMotherboardGraphicsRAM
E6300 DS3 EVGA 8600GTS 2GB XMS2 DDR2-800 
Hard DriveOSMonitorKeyboard
1.294 TB Arch Linux/XP Samsung 226bw Eclipse II 
PowerCaseMouse
Corsair 520HX Lian-Li v1000B Plus G7 
  hide details  
Reply
post #9 of 17
Thread Starter 
Quote:
Originally Posted by rabidgnome229 View Post
Creating an empty string and iteratively concatenating a single character will be painfully slow. Use java's constructor - it will be optimized.
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.
post #10 of 17
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.
BlackMesa
(14 items)
 
  
CPUMotherboardGraphicsRAM
Phenom II x6 Gigabyte XFX RF460 16gb G.Skill 
Hard DriveHard DriveOSMonitor
OCZ Vertex2 Sata II Coorsair Force GS Sata III Debian (testing) Shimian 27" 
KeyboardMouse
Filco w/ blue cherries Who needs a mouse? 
  hide details  
Reply
BlackMesa
(14 items)
 
  
CPUMotherboardGraphicsRAM
Phenom II x6 Gigabyte XFX RF460 16gb G.Skill 
Hard DriveHard DriveOSMonitor
OCZ Vertex2 Sata II Coorsair Force GS Sata III Debian (testing) Shimian 27" 
KeyboardMouse
Filco w/ blue cherries Who needs a mouse? 
  hide details  
Reply
New Posts  All Forums:Forum Nav:
  Return Home
  Back to Forum: Application Programming