我在编写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行,程序运行正常,除非该行 乘数=乘数+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)
提前感谢所有的帮助,我真的卡住了。
答案 0 :(得分:0)
不要将字符串与==符号进行比较,在if子句中使用equal()。 在您的情况下,没有打印,因为if子句中的条件返回false,因为您从扫描仪返回的字符串和您分配给“currenttarget”的字符串将通过引用进行比较,这些字符串是不同的。
错:
if((list.get(testingtarget) == currenttarget) && (list.get(testingfeature) == currentfeaturetype))
正确:
if((list.get(testingtarget).equals(currenttarget)) && (list.get(testingfeature).equals(currentfeaturetype)))
答案 1 :(得分:0)
也许你需要在结束打印后刷新并关闭你的打印本,尝试使用writer.flush();和writer.close();乘数=乘数+1之前;并且不要使用==来比较字符串使用等于
答案 2 :(得分:0)
试试useDelimiter("\\t")
。请参阅String parsing in Java with delimeter tab "\t" using split上的讨论。
示例代码:
import java.util.Scanner;
public class ScannerTest
{
public static void main(String[] args)
{
String input = "Some\ttab delimited\tstring\twhere\tsome words\tare\tseparated\tby spaces.";
Scanner sc = new Scanner(input).useDelimiter("\\t");
while (sc.hasNext())
{
System.out.println(sc.next());
}
sc.close();
}
}
输出:
Some
tab delimited
string
where
some words
are
separated
by spaces.