• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

To insert integer value into an ArrayList<String>

 
nishchita kashyap
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 18282
56
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
nishchita kashyap
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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..
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic