Your code works, however I can see a problem which will arise - two digit numbers. E.g
String s = “32 m helloworld 3 6”;
Should print out 41, however, the code above only prints 14.
I think the best solution is to use StringTokenizer as Antony has suggested.