I have a string "AB12TRHW4TR6HH58", i need to split this string and whenever i find a number, i need to perform addition of all. However, if there are consecutive numbers then i need to take it as a whole number. For example, in above string the addition should be done like 12+4+6+58 and so on.
I written below code which adds all numbers individually but cant take a whole number. Could you please help?
public class TestClass {
public static void main(String[] args) {
String str = "AB12TRHW4TR6HH58";
int len = str.length();
String[] st1 = str.split("");
int temp1=0;
for(int i=0;i<=len-1;i++){
if(st1[i].matches("[0-9]")){
int temp = Integer.parseInt(st1[i]);
temp1 = temp+temp1;
}
}
System.out.println(temp1);
}
}