Question about loadstrings() function

Hi!

Is it expected behaviour for the ‘loadstrings()’ function to skip blank or empty lines in Processing 3? The documentation doesn’t mention this, but that seems to be the case. I’m loading a small text file which has some empty lines (not even spaces), and it seems to be skipping them.

https://processing.org/reference/loadStrings_.html

Thanks.

1 Like

Good question. I believe this might be expected behavior. After some digging, it looks like loadStrings() utilizes a BufferedReader:

Inside, we can see readline() being called, and according to the docs:

Returns:
A String containing the contents of the line, not including any line-termination characters, or null if the end of the stream has been reached

In short, it’s tossing the empty lines.

1 Like

It is tossing the line terminators, but it is still indexing the lines in the array – it doesn’t “skip” them or “toss” them completely.

Given a five line text file, foo.txt:

foo

bar

baz

This code:

String[] strs = loadStrings("foo.txt");
for(int i=0; i<strs.length; i++){
  println(i, strs[i]);
}

outputs:

0 foo
1
2 bar
3
4 baz

2 Likes

Thanks. I should have done more investigation before posting. I found the problem, it has nothing to do with the loadStrings() function, but with another function that was using the array of strings. Seems it skips empty lines.

2 Likes

Glad you worked it out!

Continues: Question about GTextArea.setText() in G4P