3

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?

5
  • You don't need to check array elements for null: they will never be null. Commented Jun 13, 2013 at 18:36
  • Really, so something like array[0] = null would throw an error or something? Commented Jun 13, 2013 at 18:42
  • No, something like this can never happen with String.split() ;) If the array is empty, trying and accessing it will throw an ArrayIndexOutOfBoundsException. Arrays in Java are allocated at init time and not resizable, and a zero-length array is possible Commented Jun 13, 2013 at 18:47
  • Ah, okay, I get your point. Commented Jun 13, 2013 at 18:51
  • Does this answer your question? ArrayList - how can I check if an index exists? Commented Sep 29, 2022 at 17:07

4 Answers 4

12

You need to check length of your array, so:

if(fileAndMacro.length > 1)
   macroName = fileAndMacro[1];

By accessing an index that doesn't exist, you would be accessing some other space in memory which does not belong to your created array (actually created in the split() method), that's why you get an exception.

Sign up to request clarification or add additional context in comments.

Comments

0

Just test fileAndMacro.length. If it is 2 or larger, there was at least one exclamation mark.

1 Comment

Unfortunately, that's not quite true if you use .split() without an integer argument... Splitting "!" will give a zero length array too. You need to pass a negative argument to .split() so that it really splits by the book.
0

You can check the length of an array using its length property array.length

if (array.length  < 2){
    //perform some operation you want

}

Comments

-1

This question can apply to Kotlin as well. So, here is the solution for Kotlin:

if (index in myArray.indices) {
  // index is valid
}

Other solutions:

myArray.getOrNull(index)?.let { element ->
  // index is valid; use the element
}
if (myList.getOrNull(index) != null) {
  // index is valid
}
if (index in 0..myArray.lastIndex) {
  // index is valid
}
if (index >= 0 && index <= myArray.lastIndex) {
  // index is valid
}

There is another similar question for collections.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.