首页 > 解决方案 > remove unwanted consecutive char set if in argument(String) and return the filtered argument, else return original argument string

问题描述

the code below throws the error " 'i' cannot be resolved to a variable ", any explanations please??

static String abc(String str) {
  String[] sarr = str.split("");
  String[] newsarr = new String[sarr.length-3];
  String s = "";
  for (int i = 1; i < sarr.length; i++); {
    if ((sarr[i] == "a") && (sarr.length >= i+3)) {
  if ((sarr[i+1] == "b") && (sarr[i+2] == "c")) {
    newsarr[0] = sarr[0];
    for (int x = 1; x < i; x++) {
      newsarr[i] = sarr[i];
    }
    for (int y = i+3; y < newsarr.length; y++) {
      newsarr[y-3] = sarr[y];
    }
  } else {}
} else {}
  }
  for (String o : newsarr) {
      s += o;
  }
  return s;
}

标签: java

解决方案


删除第一个 for 循环上的分号会产生奇迹。

分号结束 for 语句和 i 的范围。


推荐阅读