Overclock.net - Overclocking.net
     
 
Home Gallery Reviews Blogs Register Today's Posts Mark Forums Read Members List


Go Back   Overclock.net - Overclocking.net > Software, Programming and Coding > Coding and Programming > Application Programming

Reply
 
LinkBack Thread Tools
Old 06-12-08   #1 (permalink)
Intel Overclocker
 
GANDALFtheGREY's Avatar
 
intel nvidia

Join Date: Nov 2004
Location: Sudbury, ON
Posts: 494

Rep: 31 GANDALFtheGREY is acknowledged by some
Unique Rep: 28
Trader Rating: 0
Default Java char[] to string?

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;
}





__________________
CPUz(E6750)_____GPUz (8800GTS)_____GPUz (9600GT)


3DMARK 06 SCORE: TBA


System: Triple Threat (Vista x64, Ubuntu, XP)
CPU
E6750 @ 3.68 1.504v
Motherboard
ASUS P5B
Memory
2GB Kingston DDR2800
Graphics Card
Palit 9600GT 512 (750/1900/2000)
Hard Drive
Seagate Barricuda 250GB 7200rpm SATA2
Sound Card
X-Fi Xtreme Audio
Power Supply
Thermaltake 550W
Case
Thermaltake MATRIX
CPU cooling
TR U120 eXtreme w/ Scythe Kaze-Jyuni
GPU cooling
Stock
OS
Vista x64 Ultimate
Monitor
20" Acer x203w
GANDALFtheGREY is offline   Reply With Quote
Old 06-12-08   #2 (permalink)
Intel Overclocker
 
GANDALFtheGREY's Avatar
 
intel nvidia

Join Date: Nov 2004
Location: Sudbury, ON
Posts: 494

Rep: 31 GANDALFtheGREY is acknowledged by some
Unique Rep: 28
Trader Rating: 0
Default

bump! it's pretty urgent lol
__________________
CPUz(E6750)_____GPUz (8800GTS)_____GPUz (9600GT)


3DMARK 06 SCORE: TBA


System: Triple Threat (Vista x64, Ubuntu, XP)
CPU
E6750 @ 3.68 1.504v
Motherboard
ASUS P5B
Memory
2GB Kingston DDR2800
Graphics Card
Palit 9600GT 512 (750/1900/2000)
Hard Drive
Seagate Barricuda 250GB 7200rpm SATA2
Sound Card
X-Fi Xtreme Audio
Power Supply
Thermaltake 550W
Case
Thermaltake MATRIX
CPU cooling
TR U120 eXtreme w/ Scythe Kaze-Jyuni
GPU cooling
Stock
OS
Vista x64 Ultimate
Monitor
20" Acer x203w
GANDALFtheGREY is offline   Reply With Quote
Old 06-12-08   #3 (permalink)
Intel Overclocker
 
GANDALFtheGREY's Avatar
 
intel nvidia

Join Date: Nov 2004
Location: Sudbury, ON
Posts: 494

Rep: 31 GANDALFtheGREY is acknowledged by some
Unique Rep: 28
Trader Rating: 0
Default

completely shameless bump to keep this on the top of the homepage!
__________________
CPUz(E6750)_____GPUz (8800GTS)_____GPUz (9600GT)


3DMARK 06 SCORE: TBA


System: Triple Threat (Vista x64, Ubuntu, XP)
CPU
E6750 @ 3.68 1.504v
Motherboard
ASUS P5B
Memory
2GB Kingston DDR2800
Graphics Card
Palit 9600GT 512 (750/1900/2000)
Hard Drive
Seagate Barricuda 250GB 7200rpm SATA2
Sound Card
X-Fi Xtreme Audio
Power Supply
Thermaltake 550W
Case
Thermaltake MATRIX
CPU cooling
TR U120 eXtreme w/ Scythe Kaze-Jyuni
GPU cooling
Stock
OS
Vista x64 Ultimate
Monitor
20" Acer x203w
GANDALFtheGREY is offline   Reply With Quote
Old 06-12-08   #4 (permalink)
Kernel Sanders
 
rabidgnome229's Avatar
 
intel nvidia

Join Date: Feb 2006
Location: Pittsburgh
Posts: 4,894
Blog Entries: 1

Rep: 548 rabidgnome229 is becoming famousrabidgnome229 is becoming famousrabidgnome229 is becoming famousrabidgnome229 is becoming famousrabidgnome229 is becoming famousrabidgnome229 is becoming famous
Unique Rep: 326
FAQs Submitted: 6
Trader Rating: 5
Default

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
__________________
BIG BROTHER
I put on my robe and wizard hat...

IS WATCHING

System: It goes to eleven
CPU
E6300
Motherboard
DS3
Memory
2GB XMS2 DDR2-800
Graphics Card
EVGA 8600GTS
Hard Drive
1.294 TB
Sound Card
Audigy 2 ZS
Power Supply
Corsair 520HX
Case
Lian-Li v1000B Plus
CPU cooling
TTBT
GPU cooling
Thermalright V2
OS
Arch Linux/XP
Monitor
Samsung 226bw

Last edited by rabidgnome229 : 06-12-08 at 08:05 PM.
rabidgnome229 is offline Overclocked Account   Reply With Quote
Old 06-12-08   #5 (permalink)
Overclocker
 
voice's Avatar
 
intel nvidia

Join Date: Oct 2007
Location: Britain
Posts: 876

Rep: 27 voice is acknowledged by some
Unique Rep: 24
Trader Rating: 0
Default

I'll check it out and try to get back to you....

EDIT: BTW stop bumping.

System: The Dust Magnet
CPU
Intel E2140 @ 3.0Ghz
Motherboard
Abit IP35 Pro
Memory
Crucial Ballistix 2GB PC2-8500 DDR2
Graphics Card
BFG 8800GT
Hard Drive
Seagate Barracuda 7200.10 250GB SATA-II 16MB Cache
Sound Card
Creative X-fi Xtreme Gamer
Power Supply
Corsair HX 620W
Case
Antec Nine Hundred
CPU cooling
TRUE w/ Xilence Redwing 120mm
OS
Windows XP SP2 & Ubuntu 7.10
Monitor
Hyundai B71A 17" LCD
voice is offline   Reply With Quote
Old 06-12-08   #6 (permalink)
Intel Overclocker
 
GANDALFtheGREY's Avatar
 
intel nvidia

Join Date: Nov 2004
Location: Sudbury, ON
Posts: 494

Rep: 31 GANDALFtheGREY is acknowledged by some
Unique Rep: 28
Trader Rating: 0
Default

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
__________________
CPUz(E6750)_____GPUz (8800GTS)_____GPUz (9600GT)


3DMARK 06 SCORE: TBA


System: Triple Threat (Vista x64, Ubuntu, XP)
CPU
E6750 @ 3.68 1.504v
Motherboard
ASUS P5B
Memory
2GB Kingston DDR2800
Graphics Card
Palit 9600GT 512 (750/1900/2000)
Hard Drive
Seagate Barricuda 250GB 7200rpm SATA2
Sound Card
X-Fi Xtreme Audio
Power Supply
Thermaltake 550W
Case
Thermaltake MATRIX
CPU cooling
TR U120 eXtreme w/ Scythe Kaze-Jyuni
GPU cooling
Stock
OS
Vista x64 Ultimate
Monitor
20" Acer x203w

Last edited by GANDALFtheGREY : 06-12-08 at 08:12 PM.
GANDALFtheGREY is offline   Reply With Quote
Old 06-12-08   #7 (permalink)
Linux Lobbyist
 
FearMeansControl's Avatar
 
intel nvidia

Join Date: Aug 2007
Location: Wrong Island, NY
Posts: 930

Rep: 66 FearMeansControl is acknowledged by some
Unique Rep: 57
Folding Team Rank: 125
Trader Rating: 2
Default

Quote:
Originally Posted by rabidgnome229 View Post
For future reference check the Java API. It's a fantastic resource
lifesaver of a link
__________________
Quote:
Originally Posted by ENTERPRISE View Post
I would reccomend opening task manager and closing the process ''IntelFanboy.exe''
Q6600
(450x8) (417x9) (425x9)

System: Jesus PC v5.3
CPU
Q6600
Motherboard
Gigabyte X38-DS4
Memory
OCZ PC-8000 2x2 GB
Graphics Card
Quadro FX-4600 & FX-1400
Hard Drive
3x2500JS RAID 0, 2x7200.11 500GB RAID 1
Sound Card
Presonus Firepod + Yamaha HS-5M's
Power Supply
OCZ StealthxStream 600W
Case
RocketPwn
CPU cooling
Fuzion v2, mcw-30, GTX 360, D5, CD Waterfall Res.
GPU cooling
Stock, re-applied with AS5
OS
Gentoo, XP x64
Monitor
Samsung Syncmaster 2253LW + 2x17" LCD's
FearMeansControl is offline I fold for Overclock.net   Reply With Quote
Old 06-12-08   #8 (permalink)
Kernel Sanders
 
rabidgnome229's Avatar
 
intel nvidia

Join Date: Feb 2006
Location: Pittsburgh
Posts: 4,894
Blog Entries: 1

Rep: 548 rabidgnome229 is becoming famousrabidgnome229 is becoming famousrabidgnome229 is becoming famousrabidgnome229 is becoming famousrabidgnome229 is becoming famousrabidgnome229 is becoming famous
Unique Rep: 326
FAQs Submitted: 6
Trader Rating: 5
Default

Creating an empty string and iteratively concatenating a single character will be painfully slow. Use java's constructor - it will be optimized.
__________________
BIG BROTHER
I put on my robe and wizard hat...

IS WATCHING

System: It goes to eleven
CPU
E6300
Motherboard
DS3
Memory
2GB XMS2 DDR2-800
Graphics Card
EVGA 8600GTS
Hard Drive
1.294 TB
Sound Card
Audigy 2 ZS
Power Supply
Corsair 520HX
Case
Lian-Li v1000B Plus
CPU cooling
TTBT
GPU cooling
Thermalright V2
OS
Arch Linux/XP
Monitor
Samsung 226bw
rabidgnome229 is offline Overclocked Account   Reply With Quote
Old 06-12-08   #9 (permalink)
Intel Overclocker
 
GANDALFtheGREY's Avatar
 
intel nvidia

Join Date: Nov 2004
Location: Sudbury, ON
Posts: 494

Rep: 31 GANDALFtheGREY is acknowledged by some
Unique Rep: 28
Trader Rating: 0
Default

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.
__________________
CPUz(E6750)_____GPUz (8800GTS)_____GPUz (9600GT)


3DMARK 06 SCORE: TBA


System: Triple Threat (Vista x64, Ubuntu, XP)
CPU
E6750 @ 3.68 1.504v
Motherboard
ASUS P5B
Memory
2GB Kingston DDR2800
Graphics Card
Palit 9600GT 512 (750/1900/2000)
Hard Drive
Seagate Barricuda 250GB 7200rpm SATA2
Sound Card
X-Fi Xtreme Audio
Power Supply
Thermaltake 550W
Case
Thermaltake MATRIX
CPU cooling
TR U120 eXtreme w/ Scythe Kaze-Jyuni
GPU cooling
Stock
OS
Vista x64 Ultimate
Monitor
20" Acer x203w
GANDALFtheGREY is offline   Reply With Quote
Old 06-12-08   #10 (permalink)
Fear is the heart of Love
 
dangerousHobo's Avatar
 
amd nvidia

Join Date: Dec 2005
Location: ~/
Posts: 3,399

FAQs Submitted: 7
Folding Team Rank: 286
Trader Rating: 0
Default

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:
Originally Posted by Melcar
Only one reasonable way to solve this... a dance off.

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

System: Anomaly
CPU
Athlon 3700 SD(KACAE)0546 @3.02ghz
Motherboard
DFI UT nF4 Ultra-D
Memory
G.Skill 2x512 UTT(BH-5)
Graphics Card
evga 6800gs
Hard Drive
Maxtor 300GB + WD 250GB
Sound Card
onboard
Power Supply
Ultra 500w V-series
Case
one from Ultra
CPU cooling
Big Typhoon
GPU cooling
80mm fan mounted on
OS
Arch Linux, Slackware 12.1
Monitor
Acer AL2216W 22" WS LCD
dangerousHobo is offline I fold for Overclock.net Overclocked Account dangerousHobo's Gallery   Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools



All times are GMT -4. The time now is 12:11 AM.


Overclock.net is a Carbon Neutral Site Creative Commons License Internet Security By ControlScan

Terms of Service / Forum Rules | Privacy Policy | Advertising | Become an Official Vendor
Copyright © 2008 Shogun Interactive Development. Most rights reserved.
Page generated in 0.22022 seconds with 9 queries