java - Scanner nextInt() doesn't return all integers in string? -
i have string currline = "0 1435 " 3029 " "(1975.92)" " 72,304""
(there quotations within string) , want print out of integers in currline. code below, number 0 printed out. how use nextint() prints out of integers?
scanner scanline = new scanner(currline); while (scanline.hasnext()) { if (scanline.hasnextint()) { system.out.println(scanline.nextint()); } scanline.next(); }
as scanner
encounters isn't int, hasnextint()
returns false
. not mention fact you're skipping on valid ints scanline.next()
call @ bottom of while
-loop. can use matcher
instead:
matcher m = pattern.compile("\\d+").matcher(currline); while (m.find()) { system.out.println(m.group()); }
0 1435 3029 1975 92 72 304
Comments
Post a Comment