• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

To insert integer value into an ArrayList<String>

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Saloon Keeper
Posts: 27807
196
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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..
 
reply
    Bookmark Topic Watch Topic
  • New Topic