aspose file tools*
The moose likes JSF and the fly likes To insert integer value into an ArrayList<String> Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » JSF
Bookmark "To insert integer value into an ArrayList<String>" Watch "To insert integer value into an ArrayList<String>" New topic
Author

To insert integer value into an ArrayList<String>

nishchita kashyap
Greenhorn

Joined: Aug 17, 2009
Posts: 2
Hi,

Is there any way to insert integer values into an ArrayList<string> ?

ArrayList<String> loc=new ArrayList<String>();
loc.add("1");

This works fine.

int randnum= (int) (Math.random() * 5);

Now, how do I insert the randnum into loc?

This might seem trivial, but its causing a huge problem in my code. Please help.
Thanks in advance,
Nishchita.
zul' Zorander
Greenhorn

Joined: Aug 12, 2009
Posts: 21
ArrayList<String> loc=new ArrayList<String>();
int randnum= (int) (Math.random() * 5);
System.out.println(randnum);
Integer i=new Integer(randnum);
loc.add(i.toString());
System.out.println(loc.get(0));
Wrapping int to a integer object and converting integer object to string
Hope this helps
Regards
Zeddicus
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 16139
    
  21

Nischita,

Welcome to the JavaRanch! We have several dozen forums here, and each forum covers a specific topic. This forum covers JavaServerFaces.

We ask that when you ask a question, you ask it in appropriate forum. You'll find more experts on that topic in the proper forum, you won't clutter forums with questions that have nothing to do with the forum topic, and people who have the same question will be able to find the question and its answer more easily. We have a FAQ on how to use the JavaRanch more effectively.

You can't literally insert an integer into a list of Strings, since that would violate the data typing rules. However, you can convert an integer value into its string representation and store that string in the string list.


Customer surveys are for companies who didn't pay proper attention to begin with.
nishchita kashyap
Greenhorn

Joined: Aug 17, 2009
Posts: 2
Hey thanks a lot for your help. It worked!

Next time I have a question I'll make sure I post it in the proper forum. Sorry about that..
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: To insert integer value into an ArrayList<String>