Вопрос

why is there an IndexOutofBoundsException? It seems as if there is no variable that index may be out of bounds. This program is supposed to convert patterns. Could this have something to do with how I read my file? Thanks.

  static int checkNestedParenFront(String line){
    int count=0;
    for(int i=0;i<line.length();i++ ){
        if(line.charAt(i)=='(')
            count++;
        if(line.charAt(i)==')'&&count==0)
            return i;
        if(line.charAt(i)==')')
            count--;
    }
    return 0;
}
 String line = new String(Files.readAllBytes(Paths.get("new.txt")));
    PrintWriter writer = new PrintWriter("old.txt");
 while(line.contains("F_4")){
            while(line.contains("$F_4$"))  {
                line=line.replace("$F_4$", "$\\AppellFiv");
        }
            int posAppell = line.indexOf("F_4");
            int posSemi = line.indexOf(';', posAppell);
            posSemi = line.indexOf(';', posSemi);
            int posComma = line.indexOf(',', posSemi);
            String check = line.substring(posComma+1);
            int i = checkNestedParenFront(check);
            String lastAppel = line.substring(posComma, i);
            String beforeAppel=line.substring(0, posComma);
            String afterAppel = line.substring(i+1);
             line = line.replaceAll("F_4[^(]*\\(([^,]+),([^;]+);([^,]+),([^;]+);([^,]+),", "\\AppellFiv@{$1}{$2}{$3}{$4}{$5}");
             line = beforeAppel + "{" + lastAppel + "}" + afterAppel;
        }
Это было полезно?

Решение

Your problem is that when you call checkNestedParenFront(check), you're returning the count in check of that last parenthesis, which is not the same as the count in line, because check starts partway through line.

Then when you call line.substring(posComma, i), you have i less than posComma, and that causes the exception.

I think you mean to have line.substring(posComma, posComma + i + 1) - but I'm not quite sure about the +1, since it's not clear exactly what you're trying to achieve.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top