2

Possible Duplicate:
Read large amount of data from file in Java

I have a string like "2 -56 0 78 0 4568 -89..." end so on. Now I use Scanner.nextInt() to parse it, but it seems to be slow. Platform is Android. Any advices how to implement in for better speed?

Thanks.

Community
  • 1
  • 1
Pavel Oganesyan
  • 6,584
  • 4
  • 48
  • 81

5 Answers5

8

use myString.split(" "), which split on ' ', then Integer.valuesOf(..)

cl-r
  • 1,254
  • 12
  • 24
3

Split the string on the space and parse each entry

for (String s: string.split(" ")) {
  int i = Integer.parseInt(s);
  //do something with i
}
Dan
  • 1,012
  • 5
  • 12
2

You could go for reading the input as normal String and then using Integer.parseInt() on it.

Kazekage Gaara
  • 14,764
  • 14
  • 55
  • 105
2

You can use String.split("\\s") and then for each string use Integer.valueOf() to parse to an Integer or Integer.parseInt() to parse to an int.

Dan D.
  • 32,096
  • 5
  • 61
  • 79
1
  you can use Integer.parseInt(Value of String);
G M Ramesh
  • 3,382
  • 9
  • 35
  • 53