我正在编写的 java 程序有问题。它旨在解析制表符分隔的文本文件,然后将它们存储到数组列表中。我正在尝试使用 java 程序更改制表符分隔的文本文件的格式。但是,我的代码出现了一个奇怪的运行时错误。这是我的代码
import java.io.*;
import java.util.*;
public class Parse
{
public static void main(String[] args) {
try{
Scanner scanner = new Scanner(new File("C:\\Users\\Nelson Liu\\My Documents\\ParsePlanets\\SearchResults.txt"));
ArrayList < String > list = new ArrayList < String > ();
scanner.useDelimiter("\t");
while (scanner.hasNext()) {
list.add(scanner.next());
}
scanner.close();
String currenttarget = "Mercury";
String currentfeaturetype = "Albedo Feature";
String featuretypebeforecomma = "albedo";
PrintWriter writer = new PrintWriter("jpl-" + currenttarget + "-" + featuretypebeforecomma + ".txt", "UTF-8");
String classname = "jpl-" + currenttarget + "-" + featuretypebeforecomma;
int multiplier = 0;
int j = 0;
int testingtarget = 0;
int testingfeature = 0;
while (multiplier < 15145) {
testingtarget = (j+2) + (10*multiplier);
testingfeature = (j + 6) +(10*multiplier);
if((list.get(testingtarget) == currenttarget) && (list.get(testingfeature) == currentfeaturetype)){
writer.print(classname);
writer.print("\t");
writer.print(list.get((j+2) + (10*multiplier)-1));
writer.print("\t");
writer.print("CODE");
writer.print("\t");
writer.print(list.get((j+2) + (10*multiplier)-2));
writer.print("\t");
writer.print("\n");
writer.print(classname);
writer.print("\t");
writer.print(list.get((j+2) + (10*multiplier)-1));
writer.print("\t");
writer.print("TARGET");
writer.print("\t");
writer.print(list.get((j+2) + (10*multiplier)));
writer.print("\t");
writer.print("\n");
writer.print(classname);
writer.print("\t");
writer.print(list.get((j+2) + (10*multiplier)-1));
writer.print("\t");
writer.print("DIAMETER");
writer.print("\t");
writer.print(list.get((j+2) + (10*multiplier)+1));
writer.print("\t");
writer.print("\n");
writer.print(classname);
writer.print("\t");
writer.print(list.get((j+2) + (10*multiplier)-1));
writer.print("\t");
writer.print("LATITUDE-LONGITUDE");
writer.print("\t");
writer.print(list.get((j+2) + (10*multiplier)+5));
writer.print(list.get((j+2) + (10*multiplier)+5) + "," + list.get((j+2) + (10*multiplier)+6));
writer.print("\t");
writer.print("\n");
writer.print(classname);
writer.print("\t");
writer.print(list.get((j+2) + (10*multiplier)-1));
writer.print("\t");
writer.print("FEATURE-TYPE");
writer.print("\t");
writer.print(list.get((j+2) + (10*multiplier)+4));
writer.print("\t");
writer.print("\n");
writer.print(classname);
writer.print("\t");
writer.print(list.get((j+2) + (10*multiplier)-1));
writer.print("\t");
writer.print("APPROVAL-STATUS");
writer.print("\t");
writer.print(list.get((j+2) + (10*multiplier)+5));
writer.print("\t");
writer.print("\n");
writer.print(classname);
writer.print("\t");
writer.print(list.get((j+2) + (10*multiplier)-1));
writer.print("\t");
writer.print("APPROVAL-DATE");
writer.print("\t");
writer.print(list.get((j+2) + (10*multiplier)+6));
writer.print("\t");
writer.print("\n");
writer.print(classname);
writer.print("\t");
writer.print(list.get((j+2) + (10*multiplier)-1));
writer.print("\t");
writer.print("ORIGIN");
writer.print("\t");
writer.print(list.get((j+2) + (10*multiplier)+7));
writer.print("\t");
writer.print("\n");
}
multiplier = multiplier +1 ;
}
} catch(Exception e){
System.out.println("error");
}
}
}
我已经取消了 1 行 1 的注释,并且程序运行良好,除非 line multiplier = multiplier +1 ; 存在
更奇怪的是,我尝试在我的 IDE 中使用调试器,只是让主类显示为空白。
堆栈跟踪(从评论中复制):
java.lang.IndexOutOfBoundsException: Index: 432, Size: 427 at
java.util.ArrayList.rangeCheck(ArrayList.java:604) at
java.util.ArrayList.get(ArrayList.java:382) at Parse.main(Parse.java:28) at
__SHELL4.run(__SHELL4.java:6) at
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) at
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at
java.lang.reflect.Method.invoke(Method.java:606) at
bluej.runtime.ExecServer$3.run(ExecServer.java:725)
提前感谢所有帮助,我真的被困住了。