检查Java中连续重复的字符

时间:2020-01-25 23:14:53

标签: java stringindexoutofbounds

我对Java很陌生。我想知道是否可以检查字符串中或字符串数​​组中的索引中是否有一定数量的连续重复字符(“确定的数字”由用户确定)。到目前为止,我已经尝试过

int multiple_characters = 0;
String array1 [] = {"abc","aabc","xyyyxy"};
for (int index = 0; index < array1.length;i++){
   for (int i = 0;i<array1[index].length;i++){
      if (array1[index].charAt(i) == array1[index].charAt(i+1)){
         multiple_characters++;
      }
   }
}

但是,我得到一个StringIndexOutOfBounds错误。我尝试通过添加一个额外的if语句来解决此问题,以确保我不等于array1[index].length,但这仍然引发相同的错误。除了以下手动和复制方法之外:

if ((array1[index].charAt(i) == array1[index].charAt(i+1) && (array1[index].charAt(i) == array1[index].charAt(i+2))

并且重复多次(这对快速更改我的代码不是很好),我似乎找不到解决方法。

3 个答案:

答案 0 :(得分:2)

对于内部for循环(带有i变量的循环),您要调用string.charAt(i+1),其中i i从0循环到该字符串的长度。

难怪您会获得超出范围的索引数组异常,您要问的最后一个字符 AFTER

我建议您尝试了解该异常,如果无法理解,请调试代码(逐步执行该代码,一次一行,如果您不知道如何使用调试器,请添加println语句,检查代码的功能与您认为的相符。代码的行为与您期望的不同?这就是错误所在。

“哦,它不起作用,我将其完全剔除,然后找到另一种方法来实现”的计划是次优的:) –回到第一个代码段,然后进行修复。

答案 1 :(得分:1)

您之所以得到StringIndexOutOfBoundsException,是因为您尝试访问string.charAt(i + 1),其中i上升到string.length() - 1的最高索引(即string)。

您可以执行以下操作:

class Main {
    public static void main(String[] args) {
        int multiple_characters = 0;
        int i;
        String array1[] = { "abc", "aabc", "xyyyxy" };
        for (int index = 0; index < array1.length; index++) {
            System.out.println("String: " + array1[index]);
            for (i = 0; i < array1[index].length() - 1; i++) {
                multiple_characters = 1;
                while (array1[index].charAt(i) == array1[index].charAt(i + 1) && i < array1[index].length() - 1) {
                    multiple_characters++;
                    i++;
                }
                System.out.println(array1[index].charAt(i) + " has been repeated consecutively " + multiple_characters
                        + " time(s)");
            }
            if (multiple_characters == 1) {
                System.out.println(array1[index].charAt(i) + " has been repeated consecutively 1 time(s)");
            }
            System.out.println("------------");
        }
    }
}

输出:

String: abc
a has been repeated consecutively 1 time(s)
b has been repeated consecutively 1 time(s)
c has been repeated consecutively 1 time(s)
------------
String: aabc
a has been repeated consecutively 2 time(s)
b has been repeated consecutively 1 time(s)
c has been repeated consecutively 1 time(s)
------------
String: xyyyxy
x has been repeated consecutively 1 time(s)
y has been repeated consecutively 3 time(s)
x has been repeated consecutively 1 time(s)
y has been repeated consecutively 1 time(s)
------------

答案 2 :(得分:1)

如果我要查找重复的字符,我会走正则表达式路线。例如,要查找重复的a字符(在此示例中重复了两次),您可以:

import java.util.regex.Pattern;

public class Temp {
  public static void main(final String[] args) {
    String array1 [] = {"abc","aabc","xyyyxy"};
    for (String item : array1){
      if (Pattern.compile("[a]{2}").matcher(item).find()) {
        System.out.println(item + " matches");
      }
    }
  }
}

在此摘录中,reg exp是"[a]{2}",它查找重复两次的任何字符序列。 当然,对于更复杂的匹配,需要更复杂的正则表达式,可以在这里找到解释这一点的好资源: https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/util/regex/Pattern.html
另一个要点是,为了提高效率,通常将以下内容移动: Pattern.compile(*Pattern*) 在方法调用之外,例如到final static field

此堆栈溢出:
RegEx No more than 2 identical consecutive characters and a-Z and 0-9
给出了与此问题有关的正则表达式问题的详细描述。

相关问题