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,774
  • 4
  • 46
  • 84

5 Answers5

8

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

cl-r
  • 1,264
  • 1
  • 12
  • 26
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,030
  • 5
  • 12
2

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

Kazekage Gaara
  • 14,972
  • 14
  • 61
  • 108
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,246
  • 5
  • 63
  • 79
1
  you can use Integer.parseInt(Value of String);
G M Ramesh
  • 3,420
  • 9
  • 37
  • 53