字符串索引超出Bounds Exception错误

时间:2015-03-23 17:39:45

标签: java exception indexing

我正在创建一个程序来读取.java文件并从中提取所有注释并将其写入html文件。该文件主要工作,但我无法提取每个方法的名称,因为它混淆了#34; Exception {"作为一个单独的类/方法。

到目前为止,这是我的代码,我相信它几乎已经完成。

http://pastebin.com/qrbJAaW3

    public class ParseDoc {

            static String fileName = null;
            static String outputR = "";
            static String inputR = "";
            static String[] lines;
            static String [] classnames;
            static StringBuilder classn = new StringBuilder();
            static String classnam = "";
            /**
             * This Method asks the user for path to input and output file
             * then it reads the file and places it on a string for easier sorting.
             * The string is sorted line by line into an array and cleaned.
             * @return Array of DOC comments
             * @throws Exception
             */
            public static String[] scanread() throws Exception{

                    System.out.println("NOTICE: If a valid file path is not entered, the program will not return a DOCHTML document.");
                    Scanner inputReader = new Scanner(System.in);
                    System.out.println("Please enter path to input file (ex: C:/Program Files/Code.java : " );
                    inputR = inputReader.nextLine();

                    System.out.println("Please enter path to html output file (ex: C:/Program Files/Output.html : " );
                    outputR = inputReader.nextLine();
                    inputReader.close();


                            FileReader file = new FileReader(inputR);
                            BufferedReader reader = new BufferedReader(file);
                            String line = reader.readLine();
                            //NAME OF THE SOURCE FILE
                            int index = inputR.lastIndexOf("/");
                            fileName = inputR.substring(index + 1);



                            int z = 0;
                            //This loop turns the input file into an String for easier access
                            while (line!= null){
                                    line = reader.readLine();
                                    z += 1;
                            }
                            reader.close();
                            file.close();

                            FileReader file2 = new FileReader(inputR);
                            BufferedReader reader2 = new BufferedReader(file2);
                            String line2 = reader2.readLine();


                            lines = new String[z];

                            int j = 0;

                            while(line2 != null)
                                            {
                                                    line2 = line2.trim();

                                                    lines[j] = line2;
                                                    line2 = reader2.readLine();
                                                    j += 1;
                                            }


                            reader2.close();
                            file2.close();

                    return lines;
            }

            /**
             * Removes all the comments from the Array containing strings
             * @param lines contains strings made of input file
             * @return Array with removed strings
             */
            public static String[] removeComments(String[] lines){


                    for (int i=0; i <lines.length;i++){
                            if (lines[i].startsWith("//"))
                            {
                                    lines[i]="";
                            }
                    }
                    return lines;

            }


            /**
             * This method scans the entire code for name of the classes and methods
             * along with their parameters and stores them in an Array for further use
             * @param lines
             * @return lines array without changing any content
             */
            public static String[] classNames(String[] lines)
            {

                    int total = 0;
                    String[] matches = new String[] {"public ", "class ","private "};

                    for(int b = 0; b <lines.length;b++)
                    {
                            Matcher num = Pattern.compile("\\S+\\s*\\(([^)]*)\\)").matcher(lines[b]);
                            for (int n = 0; n < 3 ;n++)
                            {
                                    if (lines[b].contains(matches[n]))
                                    {
                                            while(num.find())
                                            {
                                                    total += 1;
                                            }
                                    }
                            }
                    }
                    classnames = new String[total];

                    for(int z = 0; z<lines.length;z++)
                    {
                            Matcher mzz = Pattern.compile("\\w+\\s*\\{").matcher(lines[z]);
                            for (int k = 0; k < 3; k++)
                            {
                              if (lines[z].contains(matches[k])&& !(lines[z].contains("throws "))) //&& !(lines[z].contains("throws "))
                              {                            
                                              while(mzz.find())
                                              {            
                                                     classn.append( mzz.group()+"break");
                                              }
                              }
                            }

                    }

                    for(int z = 0; z <lines.length;z++)
                    {
                            //This matcher with Regex looks for class/method names along with any parameters inside
                            Matcher m = Pattern.compile("\\S+\\s*\\(([^)]*)\\)").matcher(lines[z]);
                            int i = 0;
                            for (int k = 0; k < 3; k++)
                            {
                              if (lines[z].contains(matches[k]) )
                              {                            
                                              while(m.find())
                                              {    
                                                     classn.append( m.group()+"break");
                                                    continue;

                                              }

                                    }

                            }
                    }

                    classnam = classn.toString();
                    classnames = classnam.split("break");
                    /*for(int step = 0; step<classnames.length;step++)
                    {
                            System.out.println(classnames[step]);
                    }*/
                    return lines;
            }




            /**
             * This method removes all the code from the Array and leaves on the
             * Doc comments intact.
             * @param lines
             * @return lines array with only comments remaining ( code is removed )
             */

            public static String[] removeCode(String[] lines)
            {
                    int rep = 0;
                    while ( rep <lines.length){
                lines[rep] = lines[rep].replaceAll("\\*", "Z");
                            if(!(lines[rep].startsWith("Z") || (lines[rep].startsWith("/")))){
                                    lines[rep]="";
                            }
                            lines[rep] = lines[rep].replaceAll("Z", "\\*");
                            rep += 1;
                    }



                    for(int num = 0; num <lines.length; num++)
                    {
                            if(lines[num].isEmpty()){
                                    lines[num] = null;
                            }
                    }

                    return lines;
            }



            /**
             * This method removes the remaining stars, slashes and properly formats each comment
             * before printing it.
             * @param lines The array contains parsed Java Doc comments
             * @return
             * @throws Exception
             */
            public static String[] writeTo(String[] lines) throws Exception
            {
                    BufferedWriter outputWriter = null;
                    outputWriter = new BufferedWriter(new FileWriter(outputR));
                    StringBuilder writeTo = new StringBuilder();
                    writeTo.append("<html>\n<body>\n<h2><mark> JAVA DOC COMMENTS</mark> </h2>\n<pre>\n"
                                                                    +"<big><b>Source File:</b> </big>" +"<big>"+ fileName+"</big>" + "\n\n");

                    for(int step = 0; step<lines.length;step++)
                    {
                            if(!(lines[step] == null))
                            {
                            lines[step] = lines[step].replace("@author", "<b>Author: </b>\n&nbsp&nbsp&nbsp");      
                            lines[step] = lines[step].replace("@since", "<b>Since: </b>\n&nbsp&nbsp&nbsp");
                            lines[step] = lines[step].replace("@version", "<b>Version: </b>\n&nbsp&nbsp&nbsp");
                            lines[step] = lines[step].replace("@param", "<b>Parameter: </b>\n&nbsp&nbsp&nbsp");
                            lines[step] = lines[step].replace("@return", "<b>Return: </b>\n&nbsp&nbsp&nbsp");
                            //lines[step] = lines[step].replace("*", "");
                            }
                    }




                    outputWriter.write(writeTo.toString());

                    //write to HTML
                    int countz = 0;
                    int comcount = 0;

                    //classnames[]
                    for(int resum = 0; resum<lines.length;resum++)
                    {

                            if(lines[resum] != null)
                            {
                                    if( lines[resum].charAt(0) == '*' )
                                    {
                                            lines[resum] = lines[resum].replace("*","");
                                    }


                            }
                    }
                    for(int i = 0; i < classnames.length; i++)
                    {
                            System.out.println(classnames[i]);
                    }

                    for(int resum = 0; resum<lines.length;resum++)
                    {
                            if(lines[resum] != null)
                            {

                                    if( lines[resum].charAt(0) == '/' )
                                    {
                                            if(lines[resum].endsWith("*"))
                                            {
                                            lines[resum] = lines[resum].replace("/**","<b>"+classnames[countz]+"</b>");
                                            countz++;
                                            }
                                    }

                                    if( lines[resum].charAt(0) == '/' )
                                    {

                                            lines[resum] = lines[resum].replace("/","\n");

                                    }      
                            }
                    }

                    /*for(int resum = 0; resum<lines.length;resum++)
                    {

                    }*/






                    for(int f = 0; f<lines.length;f++)
                    {
                            if(lines[f] != null)
                            {

                                    /*if(lines[f].startsWith("//") && lines[f].length() == 2)
                                    {
                                            lines[f] = "TEEEST";
                                            countz++;
                                    }*/



                                    outputWriter.write(lines[f]+"\n");

                            }

                    }






                    outputWriter.close();
                    return null;
            }


    }

控制台:

Please enter path to input file (ex: C:/Program Files/Code.java : 
I:\ICS4U0\DocParse\src\java_doc_parse\ParseDoc.java
Please enter path to html output file (ex: C:/Program Files/Output.html : 
I:\ICS4U0\DocParse\src\java_doc_parse\ParseDochh.html
ParseDoc {
scanread()
removeComments(String[] lines)
Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 0
    at java.lang.String.charAt(Unknown Source)
    at java_doc_parse.ParseDoc.writeTo(ParseDoc.java:285)
    at java_doc_parse.Execute.main(Execute.java:14)
classNames(String[] lines)
removeCode(String[] lines)
writeTo(String[] lines)

我不确定导致此错误的原因。有没有办法解决它或者我是否应该放弃在评论中添加类名?

很抱歉,如果我缺少一些信息,但我自己很困惑。

2 个答案:

答案 0 :(得分:1)

错误似乎是因为你在看似空字符串的地方调用charAt(0)。

你上面已经有一个空检查,我不知道它是否有效(你的行是否可以为空?),但我会将其更改为长度检查,可能与现有的空检查。

if (lines[resum] != null && lines[resum].length > 0) {

答案 1 :(得分:0)

我认为这是您的错误所在:

 for(int resum = 0; resum<lines.length;resum++)
            {
                    if(lines[resum] != null)
                    {

                            if( lines[resum].charAt(0) == '/' )
                            {
                                    if(lines[resum].endsWith("*"))
                                    {
                                    lines[resum] = lines[resum].replace("/**","<b>"+classnames[countz]+"</b>");
                                    countz++;
                                    }
                            }

                            if( lines[resum].charAt(0) == '/' )
                            {

                                    lines[resum] = lines[resum].replace("/","\n");

                            }      
                    }
            }

请尝试此操作,在进入for循环之前移动null检查:

 if(lines[0] != null)
 {
 for(int resum = 0; resum<lines.length;resum++)
            {


                            if( lines[resum].charAt(0) == '/' )
                            {
                                    if(lines[resum].endsWith("*"))
                                    {
                                    lines[resum] = lines[resum].replace("/**","<b>"+classnames[countz]+"</b>");
                                    countz++;
                                    }
                            }

                            if( lines[resum].charAt(0) == '/' )
                            {

                                    lines[resum] = lines[resum].replace("/","\n");

                            }      
                    }
            }