【问题标题】:Java - ArrayOutOfBoundsException help meJava - ArrayOutOfBoundsException 帮助我
【发布时间】:2012-07-12 01:14:25
【问题描述】:
import java.util.*;

import java.util.Arrays;

public class ScoreCalc {

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        char[] alphabet = {'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'};
        int[] score = {1,3,3,2,1,4,2,4,1,8,5,1,3,1,1,3,10,1,1,1,1,4,4,8,4,10};
        System.out.println("Enter word: ");
        String word = in.nextLine();
        int totalScore = 0;
        char[] wordArray = word.toCharArray();
        for(int i=0; i<wordArray.length; i++) {
            System.out.println(wordArray[i]);
            int index = Arrays.asList(alphabet).indexOf(wordArray[i]);
            System.out.println(index);
            totalScore = totalScore + score[index];
        }
        System.out.println(totalScore);
    }
}

这在线程“main”java.lang.ArrayIndexOutOfBoundsException 中不断出现异常:-1

因为它在数组字母表中找不到任何字符,请有人帮忙!

【问题讨论】:

  • 出现错误时 wordArray[i] 的值是多少?还要确保您使用的都是小写字母。
  • @twain249 它提供了单词中的第一个字母。所以如果我在“测验”中写下它会打印“q”。并且 index 以 -1 打印出来。

标签: java arrays exception compiler-errors


【解决方案1】:

indexOf(wordArray[i]) 返回 -1。我怀疑这是由于大写字母和/或特殊字符造成的。首先执行此操作并添加错误检查:

word.toLowerCase().toCharArray()

无论如何,我会做这样的事情,因为它更干净:

String alphabet = "abcdefghijklmnopqrstuvwxyz";

然后

int index = alphabet.indexOf(wordArray[i]);
if(index == -1) {
    // handle the special character
} else {
    totalScore += score[index];
}

【讨论】:

    【解决方案2】:

    所以我要做的第一件事就是让这一切都面向对象,如下所示:

    public class CharacterScore  //name this whatever makes you happy
    {  
       int value;  
       char character;  
    
       public CharacterScore(int value, char character)  
       {  
         this.value=value;  
         this.character=character;  
       }  //getters/setters  
    }  
    

    然后在你的主程序中你会做如下的事情:

    private static List<CharacterScore> characterScores;  
    static  
    {  
       characterScores = new ArrayList<CharacterScore>();  
       String alphabet = "abcdefghijklmnopqrstuvwxyz";  
       for(char current : alphabet.toCharArray())  
       {  
         characterScores.add(new CharacterScore((int)Math.random() *10), current));
       }  
    } 
    

    现在,当您获得用户输入时,您将word 转换为char[],执行如下代码:

    for(CharacterScore current : characterScores)  
    {  
        for(int i = 0; i <wordArray.length; i++)  
        {  
           if(current.getCharacter() == wordArray[i])  
           {  
                 recordScore(current.getValue());  
           }  
        }  
    }  
    

    这不一定是最好的方法,但我想帮助您理解这些概念。

    【讨论】:

      【解决方案3】:

      您的问题的原因是方法 Arrays.asList 的参数是通用可变参数 (T... a) 并且您使用的是原始字符数组。

      解决方案:使用对象Character[] alphabet = {'a','b', ...} 而不是原语char[] alphabet = {'a','b', ...},因为T... 不会威胁char[] alphabet 作为对象数组,而是作为一个对象,因此您的列表将只包含对该数组的引用。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-05-14
        • 1970-01-01
        • 2010-12-20
        • 2011-05-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多