Вы должны split()
ввести строку с каким-либо символом (пробел в вашем примере).
Пример того, как преобразовать String
в массив String
(используя метод split()
)
// Example input
String input = "1 2 3 4 5";
// Split elements by space
// So you receive array: {"1", "2", "3", "4", "5"}
String[] numbers = input.split(" ");
for (int position = 0; position < numbers.length; position++) {
// Get element from "position"
System.out.println(numbers[position]);
}
Пример, как преобразовать String
в массив int
// Example input
String input = "1 2 3 4 5";
// Split elements by space
// So you receive array: {"1", "2", "3", "4", "5"}
String[] strings = input.split(" ");
// Create new array for "ints" (with same size!)
int[] number = new int[strings.length];
// Convert all of the "Strings" to "ints"
for (int position = 0; position < strings.length; position++) {
number[position] = Integer.parseInt(strings[position]);
}