【问题标题】:How to handle: java.lang.ArrayIndexOutOfBoundsException: 1如何处理:java.lang.ArrayIndexOutOfBoundsException: 1
【发布时间】:2018-07-14 18:54:11
【问题描述】:

我有一个程序接收一个未缩进的代码文件,然后该程序接收指定的文件并将输出代码的缩进版本。

我不断收到 java.lang.ArrayIndexOutOfBoundsException: 1 错误。这似乎发生在我在一行上只有一个注释时,因为它拆分字符串时索引只占用 0。我有一个 if 语句来单独处理一行上的注释,但它仍然抛出例外。

是否需要执行 if 语句来检查拆分字符串是否包含超过 1 个部分?

import java.io.*;
import java.util.*;

class Program
{
public static int spaces = 0;

public static int longestLine = 0;
public static int commentSpaces;
public static String beforeComment;
public static String afterComment;

public static void main(String args[]) throws FileNotFoundException
{
    Scanner input2 = new Scanner(new File("C:\\Users\\James\\Music\\code.java")); //get text from file

    while (input2.hasNextLine() == true) { //get the longest line
        String text = input2.nextLine();

        if (text.contains("//")) {
            if (text.contains("\"//")) {
                printLine(text);
            }
            String[] parts = text.split("//");
            String codeOnly = parts[0];
            if (codeOnly.length() > longestLine) {
                longestLine = codeOnly.length();
            }
        }
        else {
            if (text.length() > longestLine) {
                longestLine = text.length();
            }
        }
        if (input2.hasNextLine() == false) {
            break;
        }
    }

    Scanner input3 = new Scanner(new File("C:\\Users\\James\\Music\\code.java"));

    while (input3.hasNextLine()) { //indent comments
        String text = input3.nextLine();

        if (text.contains("}")) {
            spaces -=2;
        }

        for (int i = 0; i < spaces; i++) {
            System.out.print(" ");
        }

        if (text.startsWith("//")){

            String justComment = text;
            commentSpaces = longestLine - spaces + 6;

            for (int i = 0; i < commentSpaces; i++) {

                System.out.print(" ");

            }
            printLine(justComment);
            System.out.println(" ");

        }

        if (text.contains("\"//")) {
            printLine(text);
        }

        if (text.contains("//")) {

            String[] parts = text.split("(?=//)");
            beforeComment = parts[0].trim(); // trim() to get rid of any spaces that are already present within the code
            afterComment = parts[1];

            printLine(beforeComment);
            commentSpaces = longestLine - beforeComment.length() - spaces + 5;
            for (int i = 0; i < commentSpaces; i++) {
                System.out.print(" ");
            }

            printLine(afterComment);
            System.out.println();
        }
        else {
            printLine(text);
            System.out.println();
        }

        if (text.contains("{")) {
            spaces +=2;
        }
    }
}

public static void printLine(String text) {
    Scanner data = new Scanner(text);
    while (data.hasNext()) {
        System.out.print(" " + data.next());
    }

}

public static void yesItContains() {
    System.out.print("It contains a string");
    System.exit(0);
}
}

【问题讨论】:

  • “我是否需要实现一个 if 语句来检查拆分字符串是否包含超过 1 个部分?” - 基本上是的。我能问你为什么浪费了 20 分钟左右的时间来问我们……而不是尝试一下吗?

标签: java arrays error-handling


【解决方案1】:

我认为如果 text"something//" 意味着它以空评论结尾,那么您的 parts 将只有长度 1。所以是的,您需要检查它,例如通过afterComment = parts.length &gt; 1 ? parts[1] : "";。请注意,"something // something else // blabla" 之类的行也可能会破坏这种逻辑。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-04-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-19
    • 1970-01-01
    • 2015-06-03
    • 1970-01-01
    相关资源
    最近更新 更多