Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

convert String to Arraylist

Status
Not open for further replies.

shonJava

Programmer
Dec 27, 2004
11
US
I have requirement where the user will paste these codes in the text area from the front end and I need to convert this string to Arraylist, can any body please help me with this..
-----------
222222
333333
444444
555555
666666
777777
888888
999999
-----------

This whole numbers come as String to and need to convert to Arraylist, i treid some ways but iam not successfull.
 
Hi,
Try this
Code:
 public static ArrayList getListFromTextArea(String textAreaValue){
       ArrayList list = new ArrayList();
       StringTokenizer tokens = new StringTokenizer(textAreaValue,"\n");
       while(tokens.hasMoreTokens()){
           list.add(tokens.nextElement());
       }
       System.out.println(list);
       return list;
   }

Cheers
Venu
 
Thanks a lot Venu, that was really very helpfull..
 
StringTokenizer isn't encouraged in new code (see it's API doc page). The split() method in String is recommended instead:
Code:
String[] pieces = textAreaValue.split( "\n" );
List<String> list = Arrays.asList( pieces );
 
Did anyone find a reason, appart from the Sun's one (don't use it because I say it) to avoid StringTokenizer?

Cheers,
Dian
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top