-1

Trying to continue iterating if (str1Number == str2Number), but the loop stops after incrementing the i variable

String getOldestVersion (String str1, String str2) {
    String[] str1Arr = str1.split("\\.");
    String[] str2Arr = str2.split("\\.");
    String result = "";
    int str1Number = 0;
    int str2Number = 0;

    for (int i = 0; i < str1Arr.length-1; i++) {
        str1Number = Integer.valueOf(str1Arr[i]);
        str2Number = Integer.valueOf(str2Arr[i]);
        if (str1Number > str2Number) {
            result = str1;
            break;
        } else if (str1Number == str2Number) {
            i++;
            continue;
        } else {
            result = str2;
            break;
        }
    }

    return (result);
} 
deHaar
  • 17,687
  • 10
  • 38
  • 51

4 Answers4

1

As said in the comments "using continue as the last statement in a loop doesn't make any sense." why?

because the continue statement is used to bypass the execution of below statements in the current iteration but in your case it's being used as the last statement.

it seems like what you want is if this condition is true --> if (str1Number == str2Number) then you don't want to execute any of the logic in the loop, in which case you can do:

for (int i = 0; i < str1Arr.length-1; i++){ // did you mean i < str1Arr.length ? 
       str1Number = Integer.valueOf(str1Arr[i]);
       str2Number = Integer.valueOf(str2Arr[i]);
       if (str1Number == str2Number) // <---- I've moved it to here 
            continue;
       if (str1Number > str2Number) {
             result = str1;
             break;
       } else {
             result = str2;
             break;
       }
}

I've also removed the i++; that was inside the if block as I "assumed" it might have been a typo/mistake.

Ousmane D.
  • 54,915
  • 8
  • 91
  • 126
0

Since, continue operation itself tell to increase iteration variable - i, you don't need to increase it there. You may simply write:

else if (str1Number == str2Number){
      continue;
}
BishalG
  • 1,414
  • 13
  • 24
  • 2
    using continue as the last statement in a loop doesn't make any sense. – baao Dec 05 '18 at 09:48
  • 1
    Yeah, there are lots of flaws in above code. But, user's question is concerned with working mechanism of **continue** operation. Thanks !! – BishalG Dec 05 '18 at 09:51
0

The continue keyword just sends control up to the top of the loop (after executing the increment part of the for construct). The condition is still evaluated though, so it will only continue the loop if the loop condition is satisfied.

Probably, in your case, i < str1Arr.length-1 is false.

rghome
  • 8,529
  • 8
  • 43
  • 62
0

Judging from your method's name and its return value, all you want to find is the input string that has the lowest integer as its version identifier in it.

For that, below should suffice. No loops needed at all

public class Foo {
  public static void main(String[] args) {
    System.out.println(getOldestVersion("abc.12", "abc.14"));
  }
  public static String getOldestVersion(String v1, String v2) {
    return parseInteger(v1) > parseInteger(v2) ? v2 : v1;
  }

  public static int parseInteger(String input) {
    return Integer.valueOf(input.replaceAll("\\D", ""));
  }
}

If your input string is something like 12.13.14.15, you could use

public class Foo {
  public static void main(String[] args) {
    System.out.println(getOldestVersion("12.18", "14.15"));
  }
  public static String getOldestVersion(String v1, String v2) {
    return Stream.of(v1.split("\\.")).mapToInt(Integer::parseInt).min().orElse(Integer.MAX_VALUE) > Stream.of(v2.split("\\.")).mapToInt(Integer::parseInt).min().orElse(Integer.MAX_VALUE)
        ? v2 : v1;
  }

  public static int parseInteger(String input) {
    return Integer.valueOf(input.replaceAll("\\D", ""));
  }
}
baao
  • 71,625
  • 17
  • 143
  • 203