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

Popular posts from this blog

android - Gradle sync Error:Configuration with name 'default' not found -

java - Andrioid studio start fail: Fatal error initializing 'null' -

html - jQuery UI Sortable - Remove placeholder after item is dropped -