【问题标题】:ArrayIndexOutOfBoundsException when finding words in an array that and with a specific letterArrayIndexOutOfBoundsException 在数组中查找具有特定字母的单词时
【发布时间】:2022-01-10 00:16:08
【问题描述】:

我正在尝试在以字母“a”结尾的数组中查找单词。我想用两个 for 循环来做,但我不断得到整数越界错误。

谁能告诉我我做错了什么?

代码:

Scanner sc = new Scanner(System.in);

System.out.println("Enter text: ");
String text = sc.nextLine();

String[] words = text.split(" ");

for (int i = 0; i < words.length; i++) {
    words[i] = words[i] + " ";
}

for (int i = 0; i < words.length ; i++) {
    for (int j = 0; j <= words[i].charAt(j); j++) {
        if (words[i].charAt(j) == 'a' && words[i].charAt(j + 1) == ' ') {
            System.out.println(words[i]);
        }
    }
}

【问题讨论】:

  • 感谢您的所有回复!最终使用了 endsWith();

标签: java string loops


【解决方案1】:

您的任务代码过多,这导致了一个 bug 潜入并隐藏在明显的视线中。作为一般规则,保持代码尽可能简单会减少错误。

删除这个,你不需要。

for (int i = 0; i < words.length; i++) {
    words[i] = words[i] + " ";
}

并删除所有这些:

for (int j = 0; j <= words[i].charAt(j); j++) {
    if( words[i].charAt(j) == 'a' && words[i].charAt(j + 1) == ' '){
        System.out.println(words[i]);
    }
}

而是将您的代码基于endsWith("a"):

for (String word : words) {
    if (word.endsWith("a")) {
        System.out.println(word);
    }
}

易于阅读和理解(因此更容易避免错误)。


更简单,由于不需要引用数组,直接使用拆分的结果:

String text = sc.nextLine();

for (String word : text.split(" ")) {
    if (word.endsWith("a")) {
        System.out.println(word);
    }
}

【讨论】:

    【解决方案2】:

    在第二个 for 循环中,您正在检查当前位置的字符,而不是单词的长度。将条件更改为words[i].length()进行修复。

    【讨论】:

      【解决方案3】:

      为了检查某些单词是否以字符a结尾,我们可以执行以下操作:

      public static void main(String[] args) {
          Scanner sc = new Scanner(System.in);
      
          // Read the input
          System.out.println("Enter text: ");
          String text = sc.nextLine();
      
          // Split the input by spaces
          String[] words = text.split(" ");
      
          // Iterate through the array of words and check if any of those ends with "a"
          for (String word : words) {
              if (word.endsWith("a")) {
                  System.out.println(word);
              }
          }
      }
      

      就这么简单,我们真的不需要嵌入式循环。

      【讨论】:

        【解决方案4】:

        试试这个

        String[] words = {"apple", "ada", "cat", "material", "recursion", "stacksa"};
        for (int i = 0; i < words.length; i++) {
            // get each word first
            String word = words[I];
            int lenOfWord = word.length();
            // check the last item in the word
            if (word.charAt(lenOfWord - 1) == 'a') {
                System.out.println("Last char is a" + word);
            }
        }
        

        【讨论】:

          【解决方案5】:

          其他答案通过循环解释逻辑。另一种实现相同目的的方法是通过 Java 流:

          Scanner sc = new Scanner(System.in);
          
          System.out.println("Enter text: ");
          String text = sc.nextLine();
          
          String[] words = text.split(" ");
          
          Arrays.stream(words).filter(w -> w.endsWith("a")).forEach(System.out::println);
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2019-05-12
            • 2014-01-23
            • 1970-01-01
            • 2014-10-27
            • 1970-01-01
            • 2017-04-13
            • 1970-01-01
            • 2015-07-14
            相关资源
            最近更新 更多