Hi, i’m trying to read a ASCII object (.obj) file and i’m having troubles.
In the file, there are some empty lines with only \r\n (CRLF) line breaks, for example, line 1 and 3 (the 1st line is 0).
When I do things with the line (is a String), the line seems to be empty so, it has to be null (or “”), but is not null.
Here is how to replicate
You are right, but the first line is not empty, is the line 1 and 3, that are empty
The problem is only by the empty lines so i’m using line 1 (second line).
The Processing statement loadStrings will load an ASCII file into a String array. Some of the array elements maybe empty Strings "" but none of the array elements will be null
So (file[1] == null) will never be true.
Strings are objects so this statement asks if they are the same object and clearly they aren’t because each element in the array is a different object. To compare the characters in these objects use if (file[1].equals(file[3])) { println(“1 & 3 are equal”); }
or if (file[1].equalsIgnoreCase(file[3])) { println(“1 & 3 are equal”); }
In order to avoid a potential null value, it’s much safer to test "" (or any String literal) against a variable when using equals(): if ( "".equals(variable) ) {}
Or better yet, use method isEmpty(): if ( variable.isEmpty() ) {}
Or alternatively, method isBlank() can some times be better than vanilla isEmpty(): if ( variable.isBlank() ) {}