473,380 Members | 1,424 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,380 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 19774
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Jim Bancroft | last post by:
Hi everyone, A basic one here, I think. I haven't found the pattern yet, but sometimes when I cast a variable to another type using the "C" style cast operator the compiler refuses to play...
3
by: Steve Teeples | last post by:
I have a method that passes in two string objects (both numerical numbers) and a string identifying their type. public bool DoCompare(string num1, string num2, string theirType) { System.Type...
23
by: René Nordby | last post by:
Hi there, Is there anyone that knows how to do the following? I have a class A and a class B, that 100% inherits from class A (this means that I don't have other code in class B, than...
9
by: Brian | last post by:
Hello! What is the proper syntax for casting? For example, how do I change an Integer in to a String if the variable is called Joe1 and has 20 assigned to it. Thanks, Brian
2
by: keithb | last post by:
I'm trying to understand C# type casting. The following code will not compile unless I Cast item to a String. private static String myData; foreach (DataRow row in MyTable.Rows) { foreach...
31
by: dragoncoder | last post by:
Consider the code class A { private: int a; }; int main(void) { A x; int* ptr = (int*)&x;
3
by: Tigger | last post by:
I have an object which could be compared to a DataTable/List which I am trying to genericify. I've spent about a day so far in refactoring and in the process gone through some hoops and hit some...
7
by: S. Lorétan | last post by:
Hi guys, Sorry for this stupid question, but I don't know why it isn't working. Here is my (example) code: namespace Test { class A { public string Label1; }
2
by: kilik3000 | last post by:
Hi All, I have a question about who one should go about casting types in C#. I have heard some writers and speakers call static casting a bad practice and recommend using "as" and "is". That's...
29
by: Tony Johansson | last post by:
Hello! Here I have two different way to use casting. Is any of these any better then the other? Or is it just a question of taste. newCards.Add((Card)sourceCard.Clone());...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?

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.