Here is my program and it's work if my input is somthing like that:
asd12jjh-23lm100k
but if I have two negative numbers one after one
asd123kk-23-51llk
the regex
expression do not work. So I need help how to change my expression to get all numbers from string with -
sign and then print them and calculate the sum of all elements.
import java.util.Scanner;
public class Task9 {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc = new Scanner(System.in);
System.out.println("Enter a string with digits,characters and sign");
String sentecen = sc.nextLine();
int sum = 0;
String []splitedNumbers = sentecen.split("[^0-9-]+");
for (int i = 1; i < splitedNumbers.length; i++) {
sum+=Integer.parseInt(splitedNumbers[i]);
}
for(String check:splitedNumbers){
System.out.println(check);
}
System.out.println(sum);
}
}