0

I have a String array of numbers that I read in from a data file using a Scanner:

6 10 13 14 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185

I take a line from the file and convert it into a String array. Is there a simple way to convert the String array into an int array without a for loop?

String indices = input.nextLine();
String[] clean = indices.split("\\s+");
karamazovbros
  • 950
  • 1
  • 11
  • 40

1 Answers1

2

Without a forloop may involve Streams and mapping :

String indices = input.nextLine();
int[] array = Arrays.stream(indices.split("\\s+")).mapToInt(Integer::parseInt).toArray();
System.out.println(Arrays.toString(array));
// [6, 10, 13, 14, 162, 163, 164, 165, 166, 167, 168, 169, 170, 171, 172, 173, 174, 175, 176, 177, 178, 179, 180, 181, 182, 183, 184, 185]
azro
  • 53,056
  • 7
  • 34
  • 70