View Single Post
Old 12-29-2011, 08:02 AM   PM User | #1
Scriptr
Regular Coder

 
Join Date: Oct 2011
Posts: 106
Thanks: 12
Thanked 0 Times in 0 Posts
Scriptr is an unknown quantity at this point
Split string ArrayList

I need to split a string. Simple:
Code:
String s = "Hello-world";
String str[] = new String[2];
str = s.split("-");
But, what happens when your string is user input?
Code:
String s = JOptionPane.showInputDialog(null, "text will be split at the spaces");
//they enter "Hello-world,-my-name-is-Susie"
String str[] = new String[2];
str = s.split("-");
This throws an out of bounds exception at 2.
What about ArrayList()?
Code:
//with the same user input
ArrayList<String> strings = new ArrayList<String>();
strings.add(s.split("-"));
The thing is that (for an ArrayList), there is no such thing as that last bit of code. What code that is real and does the job of strings.add(s.split("-")); for an ArrayList?
Scriptr is offline   Reply With Quote