-1

我创建了这个代码来美化源代码作为编码约定。但我有一个例外。它没有给出任何错误行,并且我的代码字符串最后一个花括号被转储。如何使用此代码修复它。

    String tab = "";
    String code = "class Demo{public static void main(String[] args) {System.out.println(\"ABC\");if(\"A\".equals(\"A\")){return \"A\";}System.out.println(\"ABC\");}}";
    String outtext = code;
    String repfrom = "{";
    String repto = "{\n";
    Pattern p = Pattern.compile(repfrom, Pattern.LITERAL);
    Matcher m = p.matcher(outtext);
    StringBuffer sb = new StringBuffer();
    while (m.find()) {
        tab += "\t";
        m.appendReplacement(sb, repto + tab);

    }
    m.appendTail(sb);
    String newtext = sb.toString().replace(";", ";\n" + tab + "\b");
    p = Pattern.compile("}", Pattern.LITERAL);
    m = p.matcher(newtext);

    repto = "}\n";
    sb = new StringBuffer();
    String back = "";
    String tab2 = tab;

    while (m.find()) {
        try {
            tab2 = tab2.substring(0, tab2.length() - 2);
        } catch (Exception e) {
        }

        back += "\b";
        m.appendReplacement(sb, tab2 + back + repto + tab + back);
    }
    System.out.println(sb.toString());

我的例外是...

java.lang.IllegalArgumentException: Contents must be presorted - added value 162 is less than preceding value 163
at org.netbeans.core.output2.IntList.add(IntList.java:76)
at org.netbeans.core.output2.AbstractLines.addTabAt(AbstractLines.java:1131)
at org.netbeans.core.output2.OutWriter.doWrite(OutWriter.java:445)
at org.netbeans.core.output2.OutWriter.write(OutWriter.java:499)
at java.io.PrintWriter.write(PrintWriter.java:456)
at java.io.PrintWriter.write(PrintWriter.java:473)
at org.apache.tools.ant.module.bridge.impl.ForkedJavaOverride$Copier.maybeFlush(ForkedJavaOverride.java:350)
at org.apache.tools.ant.module.bridge.impl.ForkedJavaOverride$Copier.access$000(ForkedJavaOverride.java:251)
at org.apache.tools.ant.module.bridge.impl.ForkedJavaOverride$NbRedirector$NbOutputStreamHandler.stop(ForkedJavaOverride.java:203)
at org.apache.tools.ant.taskdefs.Execute.execute(Execute.java:521)
at org.apache.tools.ant.taskdefs.Java.fork(Java.java:791)
at org.apache.tools.ant.taskdefs.Java.executeJava(Java.java:214)
at org.apache.tools.ant.taskdefs.Java.executeJava(Java.java:135)
at org.apache.tools.ant.taskdefs.Java.execute(Java.java:108)
at org.apache.tools.ant.UnknownElement.execute(UnknownElement.java:291)
at sun.reflect.GeneratedMethodAccessor116.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:601)
at org.apache.tools.ant.dispatch.DispatchUtils.execute(DispatchUtils.java:106)
at org.apache.tools.ant.Task.perform(Task.java:348)
at org.apache.tools.ant.Target.execute(Target.java:392)
at org.apache.tools.ant.Target.performTasks(Target.java:413)
at org.apache.tools.ant.Project.executeSortedTargets(Project.java:1399)
at org.apache.tools.ant.Project.executeTarget(Project.java:1368)
at org.apache.tools.ant.helper.DefaultExecutor.executeTargets(DefaultExecutor.java:41)
at org.apache.tools.ant.Project.executeTargets(Project.java:1251)
at org.apache.tools.ant.module.bridge.impl.BridgeImpl.run(BridgeImpl.java:283)
at org.apache.tools.ant.module.run.TargetExecutor.run(TargetExecutor.java:541)
at org.netbeans.core.execution.RunClassThread.run(RunClassThread.java:153)

Java 结果:-1

提前致谢

4

1 回答 1

0

堆栈跟踪仅包含来自 netbeans 的代码(我假设您正在运行它)。搜索该异常和“netbeans”显示有许多关于此和 netbeans 的开放错误 - https://netbeans.org/bugzilla/show_bug.cgi?id=201450

我会尝试不同的 IDE 或在命令行上编译它。

于 2013-09-18T12:48:03.333 回答