1

我正在编写的 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.jav‌​a:43) at 
java.lang.reflect.Method.invoke(Method.java:606) at 
bluej.runtime.ExecServer$3.run(ExecServer.java:725) 

提前感谢所有帮助,我真的被困住了。

4

3 回答 3

0

试试useDelimiter("\\t")请参阅使用 split 使用分隔符选项卡 "\t" 在 Java 中进行字符串解析中的讨论。

示例代码:

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.
于 2013-07-08T21:53:02.437 回答
0

不要将字符串与 == 符号进行比较,在 if 子句中使用 equal()。在您的情况下,没有打印,因为 if-clause 中的条件返回 false,因为您从扫描仪返回的字符串和您分配给“currenttarget”的字符串将通过引用进行比较,它们是不同的。

错误的:

if((list.get(testingtarget) == currenttarget) && (list.get(testingfeature) == currentfeaturetype))

正确的:

if((list.get(testingtarget).equals(currenttarget)) && (list.get(testingfeature).equals(currentfeaturetype)))
于 2013-07-08T21:09:36.700 回答
0

也许您需要在结束打印后刷新并关闭您的打印机,请尝试使用 writer.flush(); 和 writer.close(); 在乘数 = 乘数 +1 之前;并且不要使用 == 来比较 String 使用 equals

于 2013-07-08T21:38:31.797 回答