检查 java 中连续重复的字符

Checking for consecutively repeated characters in java

我对 java 很陌生。我想知道是否有可能在字符串或字符串数​​组中的索引中检查一定数量的连续重复字符('certain number' 由用户确定)。到目前为止我已经试过了

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))

并重复了很多次(这不利于快速更改我的代码),我似乎找不到解决方案。

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

难怪你会得到一个索引数组越界异常,你要的是最后一个 AFTER 字符。

我建议您尝试理解异常,如果不能,请调试您的代码(单步调试代码,一次一行,如果您不知道如何使用调试器,请添加println 语句,检查代码做了什么你认为它做了什么。你的代码在哪里与你的期望不同?这就是错误所在)。

这个计划“哦,它不起作用,我会完全放弃它并找到另一种方法来做到这一点”是次优的 :) – 回到第一个片段,并解决这个问题。

您正在获取 StringIndexOutOfBoundsException,因为您正在尝试访问 string.charAt(i + 1),其中 i 上升到 string 的最高索引(即 string.length() - 1) .

您可以按照以下方式进行:

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)
------------

如果要查找重复的字符,我会使用正则表达式。例如,要查找重复的 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
对这个问题涉及的正则表达式问题给出了相当详细的描述。