-3

I have a string separated by semicolon like this: "11;21;12;22;13;;14;24;15;25".

Look one value is missing after 13. I Want to populate that missing number with 13(previous value) and store it into another string.

S.B
  • 13,077
  • 10
  • 22
  • 49
  • 4
    So, where did you get stuck when attempting to tackle this problem? On Stackoverflow, asking a question should be about a specific programming problem. People will not do your homework, as this will also not serve you well in learning programming in general. So show that you made an attempt at solving the issue, and you will get the help you want – maloomeister Sep 29 '21 at 08:21
  • 1
    you can split the string with `string.split()` , iterate through the resulting array, look for an empty field, replace that field with "13" and the build another string from the array. – Juerrrgen Sep 29 '21 at 08:25
  • 3
    https://idownvotedbecau.se/noattempt/ – Boris the Spider Sep 29 '21 at 08:30

3 Answers3

0

Try this.

static final Pattern PAT = Pattern.compile("([^;]+)(;;+)");

public static void main(String[] args) {
    String input = "11;21;12;22;13;;;14;24;15;25";
    String output = PAT.matcher(input)
        .replaceAll(m -> (m.group(1) + ";").repeat(m.group(2).length()));
    System.out.println(output);
}

output:

11;21;12;22;13;13;13;14;24;15;25
-1

What have you tried so far?

You could solve this problem in many ways i think. If you like to use regular expression, you could do it that way: ([^;]*);;+.

Or you could use a simple String.split(";"); and iterate over the resulting array. Every empty string indicates that there was this ;;

Stephan
  • 42
  • 5
-1
String str = "11;21;12;22;13;;14;24;15;25";
String[] a = str.split(";");

for(int i=0; i<a.length; i++){
    if(a[i].length()==0){
        System.out.println(a[i-1]);
    }
}
S.B
  • 13,077
  • 10
  • 22
  • 49