Home > database >  Strange behaviour parsing booleans in java
Strange behaviour parsing booleans in java

Time:11-23

I am developing my very first libGDX game, is some kind of Idle game with desktop simulator. But my question is about Java in general.

In that game I would like to manage GameEvents data from a CSV file at first run, and later it would be saved to Gdx.App.Preferences as a String, that works well.

But what doesn't work as expected is what it seems to be the easiest thing, the CSV parse method, is nothing too complicated, I'm only doing a loop for each CSV field and storing it in a GameEvent. Class (all primitive data types, int, string and float) and actually it works... but it says some Booleans are false when actually they wasn't.

This is my parse method:

 private void parseEventData(String data) {

    String[] text = data.split(";");
    int i=0;
    for(String field : text) {
        switch(i){
            case 0:
                this.id=field;
                break;
            case 1:
                this.eventState=field;
                break;
            case 2:
              [...]
            case 8:
                this.mainEvent= (Boolean.getBoolean(field));
                break;
            case 9:
                this.active= (Boolean.getBoolean(field));
                break;
            case 10:
                this.continueEvent= (Boolean.getBoolean(field));
                break;
             [...]
            }
        i  ;
    }
}

To debug it i've included a pair of println(), the first one prints "data" raw as it enters with 2 GameEvents values:

1970MM01;PENDING;titlelonglonglong//subtitle//Mom//this is the maintext//is better if you could separate it in//various lines;MAIL//MAILB;1970MM02;NULL;MAIL;MAIL;true;true;true;1970;1;0.5;0.5;1;1;0;true;60 1970MM02;PENDING; childtitlewith a very very very very long title//subtitle//blah blah//booooh;MANAGER//MANAGERb//MANAGERc;NULL;NULL;MAIL;MAIL;false;true;false;1970;0;0.5;0.5;1;1;0;true;30

And this other is after parsing it in GameEvent.class, just parsed without passing to any other method...

1970MM01;PENDING;titlelonglonglong//subtitle//Mom//this is the maintext//is better if you could separate it in//various lines;MAIL//MAILB;1970MM02;NULL;MAIL;MAIL;false;false;false;1970;1.0;0.5;0.5;1.0;1.0;0.0;true;60

I've checked the documentation about Boolean.valueOf() and Boolean.getBoolean... but both give some random-looking values in some fields but works well in others...

What am I doing wrong?

P.S: I know I can do some workaround with "if" statements, and changing true/false by 0/1, or using Json, but I need to know what I've misunderstood in something so easy as a csv parser T.T

Thanks in advance!

CodePudding user response:

You’re using the wrong method.

Use parseBoolean(field), not getBoolean(field).


As per the documentation for getBoolean()

Returns true if and only if the system property named by the argument exists and is equal to, ignoring case, the string "true". A system property is accessible through getProperty, a method defined by the System class.

so unless there’s a system property named "true", whose value is "True", this will return false.

However, the documentation for parseBoolean() says:

Parses the string argument as a boolean. The boolean returned represents the value true if the string argument is not null and is equal, ignoring case, to the string "true". Otherwise, a false value is returned, including for a null argument.

which is what you want.

  • Related