Solved

Casting integer to string in Java

Posted on 2011-03-16
4
451 Views
Last Modified: 2012-05-11
Hi,
I have the following line in my code:

java.lang.String str = java.lang.Integer.parseInt(list[j]);

Open in new window


list has integers in it. However I want str to be string because I will feed it to a function and it requires string input.

How can I cast this integer to string safely?

Thanks,
0
Comment
Question by:Tolgar
  • 2
4 Comments
 
LVL 47

Assisted Solution

by:for_yan
for_yan earned 150 total points
ID: 35153739
You just sya String str = "" + list[j];
0
 
LVL 40

Accepted Solution

by:
gurvinder372 earned 200 total points
ID: 35153740
0
 
LVL 47

Expert Comment

by:for_yan
ID: 35153744

if list[j] is an integer
then when you contcatenate it with empty string it becomes
equialent string

that's why
String str = "" + list[j];
will give you what you want
0
 
LVL 92

Assisted Solution

by:objects
objects earned 150 total points
ID: 35153774
you can convert the int to a string, but you've already got the string so not really necessary as you can do the following

java.lang.String str = list[j];
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now