如何处理:java.lang.ArrayIndexOutOfBoundsException:1

时间:2018-02-04 22:42:09

标签: java arrays error-handling

我有一个程序,它接收一个未缩减代码的文件,并注释程序获取指定的文件,并输出缩进版本的代码。

我继续得到java.lang.ArrayIndexOutOfBoundsException:1错误。这似乎发生在我在一行上只有一个注释时,它分裂索引只占用0的字符串。我有一个if语句来处理对一行的评论,但它仍然会抛出例外。

我是否需要实现if语句来检查拆分字符串是否包含多于一部分?

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);
}
}

1 个答案:

答案 0 :(得分:0)

我认为如果text"something//"意味着它以空注释结尾,则parts只会有1个长度。所以是的,您需要检查它,例如通过afterComment = parts.length > 1 ? parts[1] : "";。请注意,像"something // something else // blabla"这样的行也可能会打破这种逻辑。