Skip to main content

Converting a JOptionPane string input to an int array?

No replies
Joined: 2008-12-07
Points: 0

Ok, for my current project, I am using JOptionPane for inputs. So, obviously the input must be a String. Well, what the input is, is a set of numbers separated by spaces: "0 4 8 10 9". The size of the set is user defined earlier on. So, what I need to do is convert this string into an int[] array so that each int in that string is set to one of the locations in the array. The array size is the same size as the set of numbers. The maximum value is also predefined, so for the example lets say a maximum value of 30 is possible.

So, say the user enters the set size of 10, then in the JOptionPane he inputs the ten numbers separated by spaces:
"1 3 6 2 12 8 22 15 9 30"

How do I go through that string and separate them into the different 10 locations of int[] array?

tmp = JOptionPane.showInputDialog("Enter " + setSize + " integers separated by spaces");

Then I need some code so that:

<br />
for (int i = 0; i < tmp.length; i ++){<br />
array[i] = the int taken from the string<br />

Now I know you can't just use tmp.length since there are spaces and integers longer than 1 involved, so how would I go about doing this? Will I need to use Tokenizer? Any sample code would be appreciated!