How to check if string.split () returns null
I am reading data from a file:
Some Name;1|IN03PLF;IN02SDI;IN03MAP;IN02SDA;IN01ARC
Some Other Name;2|IN01ALG;IN01ANZ
Another Name;3|
Test Testson;4|IN03MAP;IN01ARC;IN01ALG
I am using string.split () for every line I read from this file, for example:
String args[] = line.split("\\|");
String candidatArgs[] = args[0].split(";");
if (args[1] != "" || args[1] != null) {
String inscrieriString[] = args[1].split(";");
Thing: when I reach Another Name;3|
after .split("\\|")
, the second part ( args[1]
) should be empty, either null
or ""
(I really don't know).
However, I get the error Array index out of bounds on if (args[1] != "" || args[1] != null)
(again, AT: Another Name;3|
)
source to share
You need to check the length of the array args
.
String.split()
returns an array of length 1 for your third line, so that's args[1]
out of bounds. You should also use String.isEmpty()
instead != ""
.
Chances are, you can even skip additional checks - checking the length of the array should be sufficient:
if (args.length > 1) {
String inscrieriString[] = args[1].split(";");
...
}
source to share