title: String index out of Bounds Exception error
body: <p>I am making a program to read .java files and extract all comments from it and write it to an html file. The file works mostly but i am having trouble extracting the names of each method as it confuses "Exception{" as a separate class/method.</p>
  
  <p>So far this is the code i have and i believe it is almost done.</p>
  
  <p><a href="http://pastebin.com/qrbJAaW3" rel="nofollow">http://pastebin.com/qrbJAaW3</a></p>
  
  <pre><code>    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 &lt;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 &lt;lines.length;b++)
                      {
                              Matcher num = Pattern.compile("\\S+\\s*\\(([^)]*)\\)").matcher(lines[b]);
                              for (int n = 0; n &lt; 3 ;n++)
                              {
                                      if (lines[b].contains(matches[n]))
                                      {
                                              while(num.find())
                                              {
                                                      total += 1;
                                              }
                                      }
                              }
                      }
                      classnames = new String[total];
  
                      for(int z = 0; z&lt;lines.length;z++)
                      {
                              Matcher mzz = Pattern.compile("\\w+\\s*\\{").matcher(lines[z]);
                              for (int k = 0; k &lt; 3; k++)
                              {
                                if (lines[z].contains(matches[k])&amp;&amp; !(lines[z].contains("throws "))) //&amp;&amp; !(lines[z].contains("throws "))
                                {                            
                                                while(mzz.find())
                                                {            
                                                       classn.append( mzz.group()+"break");
                                                }
                                }
                              }
  
                      }
  
                      for(int z = 0; z &lt;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 &lt; 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&lt;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 &lt;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 &lt;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("&lt;html&gt;\n&lt;body&gt;\n&lt;h2&gt;&lt;mark&gt; JAVA DOC COMMENTS&lt;/mark&gt; &lt;/h2&gt;\n&lt;pre&gt;\n"
                                                                      +"&lt;big&gt;&lt;b&gt;Source File:&lt;/b&gt; &lt;/big&gt;" +"&lt;big&gt;"+ fileName+"&lt;/big&gt;" + "\n\n");
  
                      for(int step = 0; step&lt;lines.length;step++)
                      {
                              if(!(lines[step] == null))
                              {
                              lines[step] = lines[step].replace("@author", "&lt;b&gt;Author: &lt;/b&gt;\n&amp;nbsp&amp;nbsp&amp;nbsp");      
                              lines[step] = lines[step].replace("@since", "&lt;b&gt;Since: &lt;/b&gt;\n&amp;nbsp&amp;nbsp&amp;nbsp");
                              lines[step] = lines[step].replace("@version", "&lt;b&gt;Version: &lt;/b&gt;\n&amp;nbsp&amp;nbsp&amp;nbsp");
                              lines[step] = lines[step].replace("@param", "&lt;b&gt;Parameter: &lt;/b&gt;\n&amp;nbsp&amp;nbsp&amp;nbsp");
                              lines[step] = lines[step].replace("@return", "&lt;b&gt;Return: &lt;/b&gt;\n&amp;nbsp&amp;nbsp&amp;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&lt;lines.length;resum++)
                      {
  
                              if(lines[resum] != null)
                              {
                                      if( lines[resum].charAt(0) == '*' )
                                      {
                                              lines[resum] = lines[resum].replace("*","");
                                      }
  
  
                              }
                      }
                      for(int i = 0; i &lt; classnames.length; i++)
                      {
                              System.out.println(classnames[i]);
                      }
  
                      for(int resum = 0; resum&lt;lines.length;resum++)
                      {
                              if(lines[resum] != null)
                              {
  
                                      if( lines[resum].charAt(0) == '/' )
                                      {
                                              if(lines[resum].endsWith("*"))
                                              {
                                              lines[resum] = lines[resum].replace("/**","&lt;b&gt;"+classnames[countz]+"&lt;/b&gt;");
                                              countz++;
                                              }
                                      }
  
                                      if( lines[resum].charAt(0) == '/' )
                                      {
  
                                              lines[resum] = lines[resum].replace("/","\n");
  
                                      }      
                              }
                      }
  
                      /*for(int resum = 0; resum&lt;lines.length;resum++)
                      {
  
                      }*/
  
  
  
  
  
  
                      for(int f = 0; f&lt;lines.length;f++)
                      {
                              if(lines[f] != null)
                              {
  
                                      /*if(lines[f].startsWith("//") &amp;&amp; lines[f].length() == 2)
                                      {
                                              lines[f] = "TEEEST";
                                              countz++;
                                      }*/
  
  
  
                                      outputWriter.write(lines[f]+"\n");
  
                              }
  
                      }
  
  
  
  
  
  
                      outputWriter.close();
                      return null;
              }
  
  
      }
  </code></pre>
  
  <h2>Console:</h2>
  
  <pre><code>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)
  </code></pre>
  
  <hr>
  
  <p>I am not sure what is causing this error. Is there a way to fix it or should i just give up on adding class names to comments altogether? </p>
  
  <p>Sorry if i am lacking some information, but i am quite confused myself. </p>