java比较数组元素不能正常工作

qpgpyjmq  于 2021-06-26  发布在  Java
关注(0)|答案(1)|浏览(293)

我有这个任务,这是证明是一个挑战,因为我看不到的错误,这是导致错误出现。
分配描述符错误消息
错误消息:
v6 c36v2c22c6v4c28v4c6 c17v7c21v3c17v11 v1c29 v1 c3c17v7v9c5线程“main”java.lang.indexoutofboundsexception:索引:54,大小:12 at java.util.arraylist.rangecheck(arraylist。java:659)在java.util.arraylist.get(arraylist。java:435)在task1.applicationrunner.main(applicationrunner。java:94)/users/radi1/library/caches/netbeans/8.2/executor-snippets/run。xml:53:java返回:1生成失败(总时间:0秒)

Scanner sc;

    try {
        sc = new Scanner(fileObject);
        String result = "";
        while (sc.hasNext()) {
            String line = sc.nextLine();
            System.out.println(line);
            char[] lineChar = line.toCharArray();
            for (int i = 0; i < lineChar.length; i++) {
                if (lineChar[i] == ' ') {
                    result += " ";
                } else if (lineChar[i] == ' ' && lineChar[i + 1] == ' ') {
                    break;
                } else if (lineChar[i] == 'V') {
                    if (Character.isDigit(lineChar[i + 1]) && Character.isDigit(lineChar[i + 2])) {
                        String number = "" + lineChar[i + 1] + lineChar[i + 2];
                        result += vowels.get(Integer.parseInt(number));
                    } else {
                        result += vowels.get(lineChar[i + 1]);
                    }
                } else if (lineChar[i] == 'C') {
                    if (Character.isDigit(lineChar[i + 1]) && Character.isDigit(lineChar[i + 2])) {
                        String number = "" + lineChar[i + 1] + lineChar[i + 2];
                        result += consonants.get(Integer.parseInt(number));
                    } else {
                        result += consonants.get(lineChar[i + 1]);
                    }
                }
            }
        }
        System.out.println(result);
    } catch (FileNotFoundException fnfe) {
        System.out.println("System was unable to locate the file. Please try again.");
        System.exit(0);
    }
jhdbpxl9

jhdbpxl91#

当您试图读取多个1字符(可能只剩下1个字符)时,您遇到的问题可能会导致此indexoutofboundsexception。
循环从i=0到i=linechar.length-1。想一想特定行中的最后一个字符:在某行的最后一次迭代中,i=linechar.length-1如果您尝试读取linechar[i+1],则会出现这个indexoutofbounds异常。

相关问题