Is your problem loop logic or how to extract an integer from a string? Maybe a little of both?

Please take your question out of the middle of your code and restate it more clearly.