I want to add up the number which is coming in the string.Can anyone please tell me the program in JAVA? -
example: input: string s = "my favorite pet 43 12 cat 32 "
output: number = 43+12+33 = 88
how can generate output?
split --> parse --> sum up
public static void main(string[] args) { string s = "my favorite pet 43 12 cat 32 "; string split[] = s.split(" "); int sum=0; for(string str:split){ try{ int temp=integer.parseint(str); sum+=temp; }catch(exception e){ // continue; } } system.out.println(sum); }
Comments
Post a Comment