1

I'm working on an App for my company on Java (Android platform) and encountered an issue that I can't resolve. Here's the thing: by reading a text file I fill the content string with the following:

String content = "";
content = buffReader.readLine(); buffReader.close();

This work pretty fine. The content string is correctly filled with the file content. Every line data of this string is separated with a & character, so I fill the lines array like this:

String[] lines; 
lines = content.split("&");

Again, no error here. Then, each field is separated with a : character and again, I tried to fill the next strings in the same way:

String line; String[] data;
String data1 = ""; String data2 = ""; String data3 = ""; String data4 = "";

for (int i = 0; i < lines.length; i++){
    line = lines[i];
    data = line.split(":");
    data1 = data[0];
    data2 = data[1];
    data3 = data[2];
    data4 = data[3];
}

If the text is A:B:C:D the code works fine. Even if the text is A:B::D that's ok (and data3 becomes "" like I intended). However, if the text is A::: the code doens't work and shuts down the App. In this case, I can get data1 = data[0] but for each of the other steps, the error shows up.

Does anyone knows what's going on?! Do I'm missing something? Thanks a lot.

EDIT:

If anyone could explain why the code works fine with A:B::D text... I mean! It does have an empty filed, right?

Pspl
  • 1,398
  • 12
  • 23
  • "doesn't work" .. can you be more specific? – Stultuske Feb 13 '19 at 13:25
  • @Stultuske, It shut's down the app. – Pspl Feb 13 '19 at 13:26
  • @Pspl What's the error be more specific. – Steve Moretz Feb 13 '19 at 13:30
  • 3
    My guess is that you are getting IndexOutOfBoundsException because `split` by default removes empty trailing strings, so for `A:::` resulting array will contain only `{"A"}` not `{"A", "", "", ""}` (so no indexes 1,2,3). If that is the case use `split(":", -1)` as described in [Java String split removed empty values](https://stackoverflow.com/q/14602062) – Pshemo Feb 13 '19 at 13:30
  • @Pshemo, thanks a lot. That solved the problem. I just don't know why the split doesn't need this if the text is `A:B::D`. – Pspl Feb 13 '19 at 13:37
  • 1
    Because it removes *trailing* empty strings, not the ones *in the middle of non-empty elements*. For instance if you have `A:B::` then at first it will be split into array `{"A", "", "B", "", ""}` but since usually we don't need that last empty values (like when we split text file based on line separator and it has some extra empty lines at the end) Java authors decided to automatically remove that empty strings from the end (and since Java 8 sometimes even from the front - https://stackoverflow.com/q/22718744). So it creates and returns another smaller array without that "byproduct". – Pshemo Feb 13 '19 at 13:50
  • @Pshemo, thanks for the feedback. I totally get it now ;) – Pspl Feb 13 '19 at 13:51

1 Answers1

1

You should use split like this to prevent removing empty values

for (int i = 0; i < lines.length; i++){
    line = lines[i];
    data = line.split(":",-1);
    data1 = data[0];
    data2 = data[1];
    data3 = data[2];
    data4 = data[3];
}

If n is non-positive then the pattern will be applied as many times as possible and the array can have any length.

Radesh
  • 13,084
  • 4
  • 51
  • 64