472,122 Members | 1,459 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,122 software developers and data experts.

casting int as String

I want to do the following

int [] list;
String list;
i int;
.....
for (i=0; i<int.length, i++_
{
list.concat(list[i]);
list.concat(" ");
}

But list.concat expects a string, and I'm putting a int into it - how can I
make this work? How can I put an int into string.concat?

Thanks

Jul 17 '05 #1
6 19651
Substitute

String list

with

StringBuffer list = new StringBuffer();

list.append(list[i]);

When you want the resultant string

do list.toString();


B J H wrote:
I want to do the following

int [] list;
String list;
i int;
....
for (i=0; i<int.length, i++_
{
list.concat(list[i]);
list.concat(" ");
}

But list.concat expects a string, and I'm putting a int into it - how can I
make this work? How can I put an int into string.concat?

Thanks


Jul 17 '05 #2
B J H wrote:
list.concat(list[i]);


What are you trying to concatenate here? A string representing the
numerical digits in the int (ie, if list[i]=12345, you want to append
"12345" to your String), _or_ do you want to append the character with
the matching Unicode (or current codepage) value to your String (ie: if
list[i]=65, you want to append "A" to your String)?

If you can explain what it is you're trying to achieve, someone should
be able to help you better.

Brad BARCLAY

--
=-=-=-=-=-=-=-=-=
From the OS/2 WARP v4.5 Desktop of Brad BARCLAY.
The jSyncManager Project: http://www.jsyncmanager.org
Jul 17 '05 #3
Sorry,

I have an array of integers. I want to create a string of these integers
with a space in between each one.

E.g.

int [] intarray;
String integerlist;

If the array contains say
10
12
15
45
51

then i want the string to look like this
"10 12 15 45 51"

Any suggestions ?

"Brad BARCLAY" <bb******@jsyncmanager.org> wrote in message
news:Ki*****************@news01.bloor.is.net.cable .rogers.com...
B J H wrote:
list.concat(list[i]);


What are you trying to concatenate here? A string representing the
numerical digits in the int (ie, if list[i]=12345, you want to append
"12345" to your String), _or_ do you want to append the character with
the matching Unicode (or current codepage) value to your String (ie: if
list[i]=65, you want to append "A" to your String)?

If you can explain what it is you're trying to achieve, someone should
be able to help you better.

Brad BARCLAY

--
=-=-=-=-=-=-=-=-=
From the OS/2 WARP v4.5 Desktop of Brad BARCLAY.
The jSyncManager Project: http://www.jsyncmanager.org

Jul 17 '05 #4

"B J H" <no@one.com> wrote in message
news:QQ**********************@news.easynews.com...
Sorry,

I have an array of integers. I want to create a string of
these integers with a space in between each one.

E.g.

int [] intarray;
String integerlist;

If the array contains say
10
12
15
45
51

then i want the string to look like this
"10 12 15 45 51"

Any suggestions ?


int[] intarray = {10, 12, 15, 45, 51 };

StringBuffer spaceSepIntegers =
new StringBuffer(intarray.length * 2);

for (int i = 0; i < intarray.length; ++i)
spaceSepIntegers.append(Integer.toString(intarray[i])).append(" ");

String integerlist = spaceSepIntegers.toString().trim();

Crude but simple !

I hope this helps.

Anthony Borla
Jul 17 '05 #5
Thank you. I finally implemented it like this...

public String displayMarks()
{
StringBuffer list = new StringBuffer();
for (i=0; i < nummark; i++)
{
list.append(marks[i]);
list.append(" ");
}
return list.toString();
}

This seems to work fine me at the moment. The length of the array is
unknown, I hope that I don't run into problems if the array turns out to be
hundreds of integers long! :)

Thanks.

"Anthony Borla" <aj*****@bigpond.com> wrote in message
news:1y*****************@news-server.bigpond.net.au...

"B J H" <no@one.com> wrote in message
news:QQ**********************@news.easynews.com...
Sorry,

I have an array of integers. I want to create a string of
these integers with a space in between each one.

E.g.

int [] intarray;
String integerlist;

If the array contains say
10
12
15
45
51

then i want the string to look like this
"10 12 15 45 51"

Any suggestions ?


int[] intarray = {10, 12, 15, 45, 51 };

StringBuffer spaceSepIntegers =
new StringBuffer(intarray.length * 2);

for (int i = 0; i < intarray.length; ++i)
spaceSepIntegers.append(Integer.toString(intarray[i])).append(" ");

String integerlist = spaceSepIntegers.toString().trim();

Crude but simple !

I hope this helps.

Anthony Borla

Jul 17 '05 #6
B J H wrote:
If the array contains say
10
12
15
45
51

then i want the string to look like this
"10 12 15 45 51"

Any suggestions ?


First off, you have to convert from int to String. There are a few
ways of doing this. Assuming 'i' is an integer:

0) Use Integer.toString(i)
1) Use (""+i), or
2) Use something akin to the following:

StringBuffer sb=new StringBuffer();
sb.append(i);

If you're going to be appending a bunch of ints together, use the last
option (#2) above -- it's quite a bit more efficient than the other two.

Brad BARCLAY

--
=-=-=-=-=-=-=-=-=
From the OS/2 WARP v4.5 Desktop of Brad BARCLAY.
The jSyncManager Project: http://www.jsyncmanager.org

Jul 17 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

7 posts views Thread by Jim Bancroft | last post: by
3 posts views Thread by Steve Teeples | last post: by
23 posts views Thread by René Nordby | last post: by
9 posts views Thread by Brian | last post: by
2 posts views Thread by keithb | last post: by
7 posts views Thread by S. Lorétan | last post: by
2 posts views Thread by kilik3000 | last post: by
29 posts views Thread by Tony Johansson | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.