我创建了这段代码来美化源代码作为编码约定。但我有一个例外。它没有给出任何错误行,我的代码字符串最后一个大括号被转储。如何使用此代码修复它。
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
先谢谢
答案 0 :(得分:0)
stacktrace只包含来自netbeans的代码(我假设你正在运行它)。搜索该异常和“netbeans”显示有很多关于此问题和netbeans的漏洞 - https://netbeans.org/bugzilla/show_bug.cgi?id=201450
我会尝试不同的IDE或在命令行上编译它。