How to handle an empty string?

Asked

Viewed 559 times

1

I need to convert a string taken from a text file and fill in a person class where, each attribute of the class is separated by "," in the text file. Attributes are only: height, weight, age. In the text file they are as follows:

1.50,58,20

1.60,60,15

and so on and so forth... The problem is that some of these attributes are not filled and I am having problems when the last attribute is not filled.

Ex:1.50,58,20

   1.60,60,

   1.70.80.30

In line 2 the last one was not filled and I need to treat it. My code is as follows:

for(String s : result){
            String pessoa[];
            pessoa = s.split(",");
            System.out.println(pessoa[2]);

The result guard, at each position, one line of the file and I use the variable String pessoa[] to keep the parts separated by "," the problem is that when it is pessoa[2] makes the mistake java.lang.ArrayIndexOutOfBoundsException: 2 because the last position was not filled, there is not really the index.

How to handle this error? I have tried creating the variable and initializing the indexes as null or "" (vazio) but it didn’t work out.

  • 3

    Check the size of the array before using pessoa[2]

  • Delimite the amount of Strings to be returned in the split method to 3, thus: s.split(",", 3).

2 answers

4


You can solve your problem as follows, simply by adding a parameter delimiting the maximum amount of results of the method split, thus s.split(",", 3).

for(String s : result){

    String pessoa[];
    pessoa = s.split(",", 3);
    System.out.println(pessoa[2]);

}

Thus above, when trying to access the number 2 principle of the person array, an empty string will be returned and not an error as occurs in your code.

Example: example of use.

  • 1

    Yure, I thought it was funny that it worked. For me the second parameter represents the maximum number of elements in array resulting, but should not mess with the logic of Parsing. In fact, split(",") according to the boars must be equal to split(",", 0) (that also fails for this input). I did not realize the subtle difference that makes split(",", 3) or split(",", 5) returns arrays of 3 elements in this case.

  • 1

    Yes, the second pârametro represents the maximum number of elements to be returned, put in the example I used the string has 2 commas, thus forming three elements, where the latter is an empty string. If the second comma is removed the error will be returned again.

  • Yeah, what’s not in my head is why split(x, 0) be behaving differently than split(x, n) regarding the last empty element. In my view this is at least a problem with Javadoc that does not document this behavior.

  • It worked out, guys, thank you very much. I didn’t even think it would work that way with split, in my head I also thought it was just the maximum number of elements.

2

If you have no way of knowing if the index really exists, check the size of the array before accessing it. So we use loop loops, because they control the flow so that we don’t try to access a non-existent position.

You can check before displaying if that vector is 3 or larger:

for(String s : result){
    String pessoa[];
    pessoa = s.split(",");
    System.out.println(pessoa.length >= 3 ? pessoa[2] : "");
}

Where I put "" you can change to whatever is displayed if index 2 does not exist.

See working on IDEONE

Browser other questions tagged

You are not signed in. Login or sign up in order to post.