You can use regular expressions. Declare a pattern that searches for numeric groups, apply it to the input string, and then go through all the groups. Like that:
String line = "Купил 10 бананов потратил 5 рублей"; Pattern pattern = Pattern.compile("\\d+"); Matcher matcher = pattern.matcher(line); while (matcher.find()) { //Здесь matcher.group(0) -- это будет найденное число в строковом виде System.out.println("Found: " + matcher.group(0)); }
The code above should output:
Found: 10 Found: 5
More information about regular expressions is written here , a similar question, only with the words here.