Saturday, 19 September 2015

Sum of integers in the string

package com.nataraja.b;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class SumOfNumberInString {
 public static void main(String[] args) throws IOException {
  BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
  System.out.println("Enter a String:");
  String s=br.readLine();
  removeChar(s);
}
 
 public static void removeChar(String s){
  StringBuffer res=new StringBuffer();
  for(int i=0;i='0'&&s.charAt(i)<='9')){
    res.append(s.charAt(i));
   }
  }
  
  int n=Integer.parseInt(res.toString()),r=0,sum=0;
  
  while(n!=0){
   r=n%10;
   sum+=r;
   n=n/10;
  }
  System.out.println("Sum of  numbers in the string:"+sum);
 }
}

No comments:

Post a Comment