By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,541 Members | 1,109 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,541 IT Pros & Developers. It's quick & easy.

casting int as String

P: n/a
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
Share this Question
Share on Google+
6 Replies


P: n/a
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

P: n/a
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

P: n/a
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

P: n/a

"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

P: n/a
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

P: n/a
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.