Q:

Java how to parse integers with spaces

I have a string, actually user put in console next string :
10 20 30 40 50
how i can parse it to int[] ?
I tried to use Integer.parseInt(String s); and parse string with String.indexOf(char c) but i think it's too awful solution.

A:

You could use a Scanner and .nextInt(), or you could use the .split() command on the String to split it into an array of Strings and parse them separately.
For example:
Scanner scanner = new Scanner(yourString);
ArrayList<Integer> myInts = new ArrayList<Integer>();
while (scanner.hasNextInt()) {
  myInts.add(scanner.nextInt());
}

For the split:
String[] intParts = yourString.split("\\s+");
ArrayList<Integer> myInts = new ArrayList<Integer>();

for (String intPart : intParts) {
  myInts.add(Integer.parseInt(intPart));
}

