来自java String.split的奇怪返回值,不确定原因

时间:2014-02-20 14:10:19

标签: java string parsing split

我的程序读取遵循此模式的自定义生成的字符串:

#INT {0,1,2} / STRING / LONG#INT {0,1,2} / STRING / LONG#.......#

这是字符串开头和结尾的散列,并分隔每个子字符串,每个子字符串包含一个0-2的int,一个字符串名称和一个long值(取自系统时钟)。这些子值由正斜杠分隔。

我的程序成功地将主字符串拆分为函数'splitOnHash'中的哈希值,并将这些字符串分配给字符串的数组列表,我可以打印出来,这看起来效果很好。

接下来,在第二个函数中,我遍历这个新的arraylist,我想在'/'字符上拆分每个子字符串,并将每个值都粘贴到一个自定义的'HesitationEvent'对象中。这是我的代码:

package sg;

import java.util.ArrayList;
import java.util.List;
import sg.events.HesitationEvent;

public class HesitationDataReader {

private static List<String> substrings = new ArrayList<String>();
private static List<HesitationEvent> events = new ArrayList<HesitationEvent>();

private static void splitOnHash(String HesData) {
    for (String ret : HesData.split("#")) {
        if (ret!= "") substrings.add(ret);
    }
}

private static void createEventObjects() {
    int code;
    String object;
    long time;
    int c = 1;

    for (String sub : substrings) {
        System.out.println("count " + c);
        c++;
        String[] comp = sub.split("/");

        System.out.println("comp:"+comp+":comp");

        code = Integer.parseInt(comp[0]);
        object = comp[1];
        time = Long.parseLong(comp[2]);

        HesitationEvent hes = new HesitationEvent(code, object, time);
        events.add(hes);
    }
}


public static void main (String args[]) {
    String ourString = "#0/diamonds4/1392748304285#2/diamonds4/1392748304333#0/hearts7/1392748304364#2/hearts7/1392748305035#" +
            "1/deck/1392748305456#1/deck/1392748311696#1/deck/1392748313489#1/deck/1392748315490#0/clubs7/1392748317599#" +
            "2/clubs7/1392748317623#0/clubs5/1392748317623#2/clubs5/1392748317647#0/spades3/1392748317647#2/spades3/1392748323913#" +
            "1/spades3/1392748324616#2/spades3/1392748324710#0/diamonds4/1392748324710#2/diamonds4/1392748324734#0/clubs5/1392748324782#" +
            "2/clubs5/1392748325126#2/clubs5/1392748325214#1/clubs5/1392748325625#2/clubs5/1392748325782#0/spades6/1392748325806#" +
            "2/spades6/1392748325918#0/spades3/1392748326006#2/spades3/1392748326262#0/diamonds4/1392748326262#2/diamonds4/1392748326678#" +
            "2/diamonds4/1392748326830#1/diamonds4/1392748327498#2/diamonds4/1392748328094#0/spades6/1392748328118#2/spades6/1392748328206#" +
            "0/diamonds13/1392748328238#2/diamonds13/1392748328534#0/diamonds13/1392748328790#2/diamonds13/1392748329046#0/hearts7/1392748329582#" +
            "2/hearts7/1392748329942#0/hearts7/1392748330150#2/hearts7/1392748330246#0/hearts7/1392748330454#2/hearts7/1392748330654#" +
            "1/deck/1392748333057#0/spades10/1392748333990#2/spades10/1392748334006#0/clubs13/1392748334006#2/clubs13/1392748334038#" +
            "0/hearts1/1392748334038#2/hearts1/1392748334477#1/hearts1/1392748334927#2/hearts1/1392748335093#0/diamonds13/1392748335261#" +
            "2/diamonds13/1392748335325#0/hearts7/1392748335341#2/hearts7/1392748335797#2/hearts7/1392748336013#2/hearts7/1392748336237#" +
            "2/hearts7/1392748336325#2/hearts7/1392748336429#1/hearts7/1392748337240#2/hearts7/1392748337517#0/clubs4/1392748337525#" +
            "2/clubs4/1392748337557#0/diamonds4/1392748337565#2/diamonds4/1392748337573#0/clubs5/1392748337573#2/clubs5/1392748337581#" +
            "0/hearts6/1392748337581#2/hearts6/1392748337589#0/spades6/1392748337589#2/spades6/1392748337613#0/diamonds13/1392748337629#" +
            "2/diamonds13/1392748337637#0/spades10/1392748337653#2/spades10/1392748337661#0/spades10/1392748337933#2/spades10/1392748337965#" +
            "0/clubs13/1392748337965#2/clubs13/1392748338509#2/clubs13/1392748338557#1/clubs13/1392748338919#2/clubs13/1392748339237#" +
            "1/deck/1392748341879#0/clubs13/1392748342477#2/clubs13/1392748342549#0/spades6/1392748345549#2/spades6/1392748345581#" +
            "0/hearts1/1392748345637#2/hearts1/1392748345837#0/hearts1/1392748346421#2/hearts1/1392748346661#0/hearts9/1392748350302#" +
            "2/hearts9/1392748350381#0/spades11/1392748350381#2/spades11/1392748350381#0/hearts2/1392748350381#2/hearts2/1392748350397#";

    splitOnHash(ourString);
    //for (String s:substrings) {
    //  System.out.println(s);
    //}
    createEventObjects();   
    }

}

请忽略变量c和底部的for循环,我用c来确定for循环崩溃的位置(第一次迭代)。底部的for循环(注释掉)用于确认我的splitOnHash函数返回了我的预期(它看起来工作正常)。

所以基本上我试图将每个子字符串拆分成一个数组,然后将每个值拉出并转换为正确的类型。我也尝试使用split(“/”,3)来准确返回3个值,但它得到的结果相同,即:

count 1
Exception in thread "main" comp:[Ljava.lang.String;@1fae3c6:comp
java.lang.NumberFormatException: For input string: ""
    at java.lang.NumberFormatException.forInputString(Unknown Source)
    at java.lang.Integer.parseInt(Unknown Source)
    at java.lang.Integer.parseInt(Unknown Source)
    at sg.HesitationDataReader.createEventObjects(HesitationDataReader.java:30)
    at sg.HesitationDataReader.main(HesitationDataReader.java:87)

这里很难说,但是“comp:[Ljava.lang.String; @ 1fae3c6:comp”实际上是程序的输出,出于某种原因出现在异常中间。这是从我尝试打印出comp的值的行,包含comp:VALUE:comp(我这样做是为了打印空字符串或空字符串,所以我可以看到冒号之间没有间隙。 / p>

所以出于某种原因,在第一次迭代中,sub.split输出到String [] comp会产生“Ljava.lang.String; @ 1fae3c6”,当我期待它提出更像的东西时这:“0钻石4 1392748304285”。

我刚刚更改了代码以尝试访问数组中的位置而不是打印所有3个段,我收到此错误:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1
at sg.HesitationDataReader.createEventObjects(HesitationDataReader.java:29)
    at sg.HesitationDataReader.main(HesitationDataReader.java:89)

这里显然有问题,但我看不出问题!我不能使用我用来拆分哈希的原始方法,因为这次我没有以任何顺序返回字符串,我想访问特定的值并将它们转换为特定的类型。

提前感谢您的帮助!

1 个答案:

答案 0 :(得分:9)

问题在于:

(ret!= "")

应该是:

  if (!ret.equals("")) 

我认为错误导致插入空字符串。