简体   繁体   中英

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#.......#

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). 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.

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. 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). The for loop at the bottom (commented out) was used to confirm my splitOnHash function returned what I expected (it seems to work fine).

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:

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. 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.

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".

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:

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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