I'm using the str.split("!") to pull a string in half when there is an exclamation mark contained in the string. With the way the code is set up I will get a 1 index array if there is no exclamation mark and a 2 index array if there is one.
Code:
String file, macroName;
String[] fileAndMacro = string.split("!");
if(fileAndMacro[0] != null)
file = new File(fileAndMacro[0]);
if(fileAndMacro[1] != null)
macroName = fileAndMacro[1];
If I put in a string with an exclamation mark, it works. For example "test!string" would return fileAndMacro[0] = "test" and fileAndMacro[1] = "string".
The problem is when I don't have an exclamation mark (as some of you can probably tell from my code). I simply get an ArrayIndexOutOfBoundsException. So, clearly a null check is not doing the trick. Which makes sense considering there can't be a null value in memory if no space has been allocated for the value to be stored in.
Despite my understanding of this, I'm not sure how to check to see if that second index exists or not. How do I check to see if an index exists or not in real time?
array[0] = nullwould throw an error or something?String.split();) If the array is empty, trying and accessing it will throw anArrayIndexOutOfBoundsException. Arrays in Java are allocated at init time and not resizable, and a zero-length array is possible