473,694 Members | 2,663 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(lis t[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 19787
Substitute

String list

with

StringBuffer list = new StringBuffer();

list.append(lis t[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(lis t[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(lis t[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******@jsync manager.org> wrote in message
news:Ki******** *********@news0 1.bloor.is.net. cable.rogers.co m...
B J H wrote:
list.concat(lis t[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.c om...
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 spaceSepInteger s =
new StringBuffer(in tarray.length * 2);

for (int i = 0; i < intarray.length ; ++i)
spaceSepInteger s.append(Intege r.toString(inta rray[i])).append(" ");

String integerlist = spaceSepInteger s.toString().tr im();

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(mar ks[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*****@bigpon d.com> wrote in message
news:1y******** *********@news-server.bigpond. net.au...

"B J H" <no@one.com> wrote in message
news:QQ******** **************@ news.easynews.c om...
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 spaceSepInteger s =
new StringBuffer(in tarray.length * 2);

for (int i = 0; i < intarray.length ; ++i)
spaceSepInteger s.append(Intege r.toString(inta rray[i])).append(" ");

String integerlist = spaceSepInteger s.toString().tr im();

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.toStrin g(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
30778
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 along. It says it's an invalid cast. However, if I use the Convert.ToInt32() method (for example) things will work. At least, that's how it appears to me. Could someone explain when to use old-style parenthesized casts vs. the Convert() methods?
3
3574
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 type = System.Type.GetType(theirType); return ((type)num1 <= (type)num2) ? true : false; } The compiler doesn't like the (type) casting that I do. What is the proper
23
3512
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 the Inherit statement).
9
1042
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
1711
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 (Object item in row.ItemArray) {
31
3170
by: dragoncoder | last post by:
Consider the code class A { private: int a; }; int main(void) { A x; int* ptr = (int*)&x;
3
2762
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 dead ends. I'm posting this to get some feedback on wether I'm going in the right direction, and at the same time hopefully save others from going through the process.
7
13682
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
2644
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 fine. However in most of the production code that I see, programmers use either static casting OR use the "as" keyword without using the "is" keyword first to verify that the object actually supports the desired type.
29
1848
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()); newCards.Add(sourceCard.Clone() as Card); //Tony
0
8616
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8552
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
8818
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8812
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7650
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6481
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5825
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
2992
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
1970
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.