【问题标题】:Array Index Out of Bounds Exception (Java)数组索引越界异常(Java)
【发布时间】:2018-01-25 18:57:24
【问题描述】:

这是我的代码:

public class countChar {

    public static void main(String[] args) {
        int i;
        String userInput = new String();

        userInput = Input.getString("Please enter a sentence");

        int[] total = totalChars(userInput.toLowerCase());

        for (i = 0; i < total.length; i++);
        {
            if (total[i] != 0) {
                System.out.println("Letter" + (char) ('a' + i) + " count =" + total[i]);
            }
        }
    }

    public static int[] totalChars(String userInput) {
        int[] total = new int[26];
        int i;
        for (i = 0; i < userInput.length(); i++) {
            if (Character.isLetter(userInput.charAt(i))) {
                total[userInput.charAt(i) - 'a']++;
            }
        }
        return total;
    }
}

该程序的目的是向用户询问一个字符串,然后计算每个字符在该字符串中使用的次数。

当我去编译程序时,它工作正常。当我运行程序时,我可以在弹出框中输入一个字符串,但是在我提交该字符串并按确定后,我得到一个错误,说

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 26
at countChar.main(countChar.java:14)

我不完全确定问题是什么或如何解决。

【问题讨论】:

  • 我认为 for 循环应该包含 if 语句,最后的 ; 是错误的。
  • 你能指出第14行是哪一行吗? (使用// &lt;-- ArrayIndexOutOfBoundsException here)

标签: java arrays runtime-error


【解决方案1】:
for ( i = 0; i < total.length; i++ );
                                    ^-- remove the semi-colon here

使用这个分号,循环一直循环到i == total.length,什么也不做,然后执行您认为的循环体。

【讨论】:

  • 我觉得 { 的不同格式(即在 for 行的末尾)可能会使这一点变得显而易见。
  • 我更新了代码,现在我得到了这个错误:countChar.java:18: error: illegal start of expression public static int[]totalChars( String userInput ) ^第二个方法声明行上的所有其他词都会出现这个错误。
  • for (i = 0; !compiler.someoneFixesTheCompilerToWarnAboutThisError(); i++) StackOverflow.answerQuestionBySomeoneToMakeThisSameErrorAgain();i的当前值是多少?我怀疑现在已经有几千...
【解决方案2】:
for ( i = 0; i < total.length; i++ ); // remove this
{
    if (total[i]!=0)
        System.out.println( "Letter" + (char)( 'a' + i) + " count =" + total[i]);
}

for 循环循环直到 i=26(其中 26 是 total.length),然后执行您的 if,越过数组的边界。删除 for 循环末尾的 ;。

【讨论】:

    【解决方案3】:

    这是在 java 中减去数组长度的一个很好的例子,我在这里给出了两个例子

     public static int linearSearchArray(){
    
       int[] arrayOFInt = {1,7,5,55,89,1,214,78,2,0,8,2,3,4,7};
       int key = 7;
       int i = 0;
       int count = 0;
       for ( i = 0; i< arrayOFInt.length; i++){
            if ( arrayOFInt[i]  == key ){
             System.out.println("Key Found in arrayOFInt = " + arrayOFInt[i] );
             count ++;
            }
       }
    
       System.out.println("this Element found the ("+ count +") number of Times");
    return i;  
    }
    

    这上面我 < arrayOFInt.length;不需要减一数组的长度; 但是如果你我 <= arrayOFInt.length -1;有必要其他明智的 arrayOutOfIndexException 发生,希望这对你有所帮助。

    【讨论】:

      【解决方案4】:
      import java.io.*;
      import java.util.Scanner;
      class ar1 {
          public static void main(String[] args) {
              //Scanner sc=new Scanner(System.in);
              int[] a={10,20,30,40,12,32};
              int bi=0,sm=0;
              //bi=sc.nextInt();
              //sm=sc.nextInt();
              for(int i=0;i<=a.length-1;i++) {
                  if(a[i]>a[i+1]) 
                      bi=a[i];
      
                  if(a[i]<a[i+1])
                      sm=a[i];
              }
              System.out.println("big"+bi+"small"+sm);
          }
      }
      

      【讨论】:

      • 不要简单地发布代码块,请解释您的答案
      猜你喜欢
      • 2016-01-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-11-19
      相关资源
      最近更新 更多