How do i take a input from JOptionPane.showInputDialog, split it and add it to an Arraylist?
public static void main(String[] args) {
String acc = JOptionPane.showInputDialog("Enter a string:");
int num = Integer.parseInt(acc);
}
How do i take a input from JOptionPane.showInputDialog, split it and add it to an Arraylist?
public static void main(String[] args) {
String acc = JOptionPane.showInputDialog("Enter a string:");
int num = Integer.parseInt(acc);
}
Please pay more attention to your posts. In the title you say LinkedList, in the text ArrayList. By the way will nobody figure out what you really want with that kind of information.
So you want to split something? You mean the string that you are getting there? Then just look at this post!
Then with the single values you simply add then to the list.
Example:
String acc = JOptionPane.showInputDialog("Enter a string:"); //enters yes-no
String[] result = acc.split("-");
myArrayList.add(result[0]); //yes
myArrayList.add(result[1]); //no
You can use Java's String.split()
to separate a string based on a separatos.
For example if the words in the String are separated by a space then you can use:
yourString.split(" ");
This will return an String array. A more concrete example for what you want can be something like this
ArrayList<String> list = new ArrayList<String>();
String pop = "hello how are you doing";
for(String s: pop.split(" ")){
list.add(s);
}
The variable 'list' will contain:
["hello", "how", "are", "you", "doing" ]
EDIT: I read in another post that you wanted to parse it to integer first, you should put that kind of things in your question. If you can then it's better if you split the string with the above method and then parse each element as you add it to an Integer ArrayList (this if the elemenets are integers).
Component frame = new JFrame();
// get text
String name = JOptionPane.showInputDialog(frame, "What's your name?");
System.out.println(name);
// split text when u get a space
List<String> list = Arrays.asList(name.split("\\p{Z}+"));
System.out.println(list);