write a program to find sum of integers from a string


  1. import java.util.regex.Matcher;
  2. import java.util.regex.Pattern;


  3. public class GetSum {

  4. public static void main(String[] args) {
  5. String s = "1ima7 j1 1hh1  0ttr1dfg10";
  6. Pattern p = Pattern.compile("[0-9]");
  7. Matcher m = p.matcher(s);
  8. int sum = 0;

  9. while (m.find()) {
  10. sum += Integer.parseInt(m.group());
  11. }
  12. System.out.println(sum);
  13. }

  14. }

Comments

Popular posts from this blog

Today Walkin 14th-Sept

Spring Elasticsearch Operations

Hibernate Search - Elasticsearch with JSON manipulation