简体   繁体   English

Java String.split的奇怪返回值,不确定为什么

[英]Strange return value from java String.split, not sure why

My program reads a custom generated string which follows this pattern: 我的程序读取遵循此模式的自定义生成的字符串:

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

So that's a hash at the start and end of the string, and seperating each substring, with each substring containing an int from 0-2, a string name, and a long value (taken from system clock). 因此,这是字符串开头和结尾的哈希,并分隔每个子字符串,每个子字符串包含一个0到2之间的整数,一个字符串名称和一个长值(从系统时钟中获取)。 These subvalues are seperated by forward slashes. 这些子值由正斜杠分隔。

My program successfully splits the main string on the hashes in the function 'splitOnHash', and assigns these to an array list of strings, which I can print out, and this seems to work nicely. 我的程序成功地在函数'splitOnHash'中将散列中的主字符串分割开了,并将它们分配给字符串数组列表,我可以将其打印出来,这似乎很好用。

Next, in the second function, I iterate through this new arraylist, I want to split each substring on the '/' character, and stick each value into a custom 'HesitationEvent' object. 接下来,在第二个函数中,我遍历这个新的数组列表,我想在'/'字符上拆分每个子字符串,并将每个值粘贴到自定义的'HesitationEvent'对象中。 Here is my code: 这是我的代码:

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();   
    }

}

Please ignore the variable c, and the for loop at the bottom, I used c to determine at what point the for loop crashes (the very first iteration). 请忽略变量c,在底部忽略for循环,我使用c来确定for循环在哪一点崩溃(第一次迭代)。 The for loop at the bottom (commented out) was used to confirm my splitOnHash function returned what I expected (it seems to work fine). 底部的for循环(已注释掉)用于确认我的splitOnHash函数返回了我期望的结果(它似乎工作正常)。

So essentially I am trying to split each substring into an array, and then pull and convert each value to the right type. 所以从本质上讲,我试图将每个子字符串拆分成一个数组,然后将每个值提取并转换为正确的类型。 I've also tried with split("/",3) to return exactly 3 values, but it gets the same result, which is: 我还尝试了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)

It's hard to tell here, but the "comp:[Ljava.lang.String;@1fae3c6:comp" is actually output of the program which appears in the midst of the exception for some reason. 在这里很难说,但是“ comp:[Ljava.lang.String; @ 1fae3c6:comp”实际上是程序的输出,由于某种原因,它出现在异常的中间。 This is from the line where I try to print out the value of comp, surrounded by comp:VALUE:comp (I did this in case it prints an empty or null string, so I can see there is no gap between the colons. 这是从我尝试打印comp值的那一行开始,用comp:VALUE:comp包围(我这样做是在它打印出一个空字符串或null字符串的情况下,所以我可以看到冒号之间没有间隙。

So for some reason, in the first iteration, the output of sub.split into the String[] comp produces "Ljava.lang.String;@1fae3c6", when I'm expecting it to come up with something more like this: "0 diamonds4 1392748304285". 因此由于某种原因,在第一次迭代中,将sub.split转换为String [] comp的输出会产生“ Ljava.lang.String; @ 1fae3c6”,而我希望它能提供更多类似这样的信息: 0颗钻石4 1392748304285“。

I just changed the code to try and access a location in the array rather than printing all 3 segments, and I get this error: 我只是更改了代码以尝试访问数组中的某个位置,而不是打印所有3个段,但出现此错误:

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

There is obviously something wrong here, but I can't see the problem! 显然这里有问题,但是我看不到问题! I cannot use the original method I used to split the hashes, because I'm not returning strings in any order this time, I want to access specific values and convert them to specific types. 我无法使用用于拆分哈希的原始方法,因为这次我不会以任何顺序返回字符串,因此我想访问特定的值并将其转换为特定的类型。

Thanks in advance for any help! 在此先感谢您的帮助!

Here is the problem: 这是问题所在:

(ret!= "")

Should be: 应该:

  if (!ret.equals("")) 

I believe the error is causing insertion of empty strings. 我相信该错误会导致插入空字符串。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM