Java中的数组超出范围的异常

时间:2018-08-03 05:03:32

标签: java string

如何清除Java中的ArrayIndexOutOfBound异常

线程“主”中的异常java.lang.ArrayIndexOutOfBoundsException:10         在word.main(word.java:17)

我的代码如下:

        import java.util.*;
        import java.lang.*;
        class word{

    public static void main(String[] args){
        String n;
        String a[]={"zero","one","two","three","four","five","six","seven","eight","nine","ten"};
        String b[]={"eleven","twelve","thirteen","fourteen","fifteen","sixteen","seventeen","eighteen","nineteen","twenty"};
        System.out.println("Enter number in words");
        Scanner sc=new Scanner(System.in);
        n=sc.next();
        for(int i=0;i<=20;i++){
            if(n==a[i]){
                System.out.println(i);
                break;
            }
            else if(n==b[i]){

                System.out.println(i);
                break;
            }
        }
    }
}

3 个答案:

答案 0 :(得分:2)

老实说,执行此操作最简单的方法是对所有数字单词使用单个数组,或者使用更灵活的数据结构(例如列表)。如果要保留当前的内联数组定义,但要使用列表,则可以选择以下一种方法:

String[] a = { "zero", "one", "two", "three", "four", "five", "six", "seven",
               "eight", "nine", "ten" };
String[] b = { "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen",
              "seventeen", "eighteen", "nineteen", "twenty" };
List<String> nums = new ArrayList<>();
nums.addAll(Arrays.asList(a));
nums.addAll(Arrays.asList(b));

System.out.println("Enter number in words");
Scanner sc = new Scanner(System.in);
String input = sc.next();

for (int i=0; i < nums.size(); i++) {
    if (input.equals(nums.get(i))) {
        System.out.println(i);
        break;
    }
}

答案 1 :(得分:1)

  

访问索引大于或等于数组的数组   数组的长度导致“ ArrayIndexOutOfBoundsException”。

在您的代码中,您试图访问第11个(和第12个,依此类推)索引, 10个长度数组。 (A是11,B是10)。

考虑到您显然要为给定的“字母”数字输出数值,最好的解决方案似乎是在包含21个元素的单个数组上循环21次。

最后,您应该始终在循环中使用 array.length ,使用乱码(例如20)意味着您必须在编辑数组后立即对其进行编辑。

总而言之,它看起来像这样:

  public static void main(String[] args){
        String n;
        String a[]={"zero","one","two","three","four","five","six","seven","eight","nine","ten", "eleven","twelve","thirteen","fourteen","fifteen","sixteen","seventeen","eighteen","nineteen","twenty"}; 
        System.out.println("Enter number in words");
        Scanner sc=new Scanner(System.in);
        n=sc.next();
        for(int i=0;i<a.length;i++){
            if(n.equals(a[i])){
                System.out.println(i);
                break;
            }
        }
    }

答案 2 :(得分:1)

我认为有必要分开保存两个数组。正如其他人提到的那样,您正在尝试访问数组中超出其长度的位置。您已经有不错的解决方案,但是如果您想保留自己的代码,可以尝试一下。

public static void main(String[] args){
        String n;
        String a[]={"zero","one","two","three","four","five","six","seven","eight","nine","ten"};
        String b[]={"eleven","twelve","thirteen","fourteen","fifteen","sixteen","seventeen","eighteen","nineteen","twenty"};
        System.out.println("Enter number in words");
        Scanner sc=new Scanner(System.in);
        n=sc.next();
        for(int i=0;i<=20;i++){
            if(n.equals(a[i%a.length])){
                System.out.println(i);
                break;
            }
            else if(n.equals(b[i%b.length])){

                System.out.println(i);
                break;
            }
        }
    }