gpt4 book ai didi

java - 字符串索引越界异常错误

转载 作者:行者123 更新时间:2023-12-01 11:45:03 25 4
gpt4 key购买 nike

我正在编写一个程序来读取 .java 文件并从中提取所有注释并将其写入 html 文件。该文件大部分工作正常,但我在提取每个方法的名称时遇到困难,因为它将“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)
<小时/>

我不确定是什么导致了这个错误。有没有办法解决这个问题,或者我应该放弃在评论中添加类名?

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

最佳答案

该错误似乎是因为您对看似空字符串调用 charAt(0) 。

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

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

关于java - 字符串索引越界异常错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29216784/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com