Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fast way to parse a string into int in Java [duplicate]

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.

like image 275
Pavel Oganesyan Avatar asked May 13 '26 00:05

Pavel Oganesyan


2 Answers

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

like image 145
cl-r Avatar answered May 14 '26 13:05

cl-r


Split the string on the space and parse each entry

for (String s: string.split(" ")) {
  int i = Integer.parseInt(s);
  //do something with i
}
like image 45
Dan Avatar answered May 14 '26 13:05

Dan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!