• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

arrayindexoutofbound exception

 
prathi suku
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi everyone

Is there a method to find if the index of a string array exists or not. I have a String array arr[] and I want to know if the index arr[1] exists or not .
arr[1] may or may not exist.
Many thanks for your help.

 
Wouter Oet
Saloon Keeper
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
arr.length??
 
Rooks Forgenal
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I guess it depends on what the array looks like. Is it periodically filled with nulls like this? ["a", null, "b", "c", "d", null]
If so, try the code below.
 
Wouter Oet
Saloon Keeper
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But he is asking about the index not the object at that index.
 
Rooks Forgenal
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I agree that is what he is 'literally' asking. However, when I first began, I often described (incorrectly) the element of an array at an index as an index of an array. I just thought he might have done the same thing in this case.
 
prathi suku
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the replies. Yes Jake for the code solution you provided
boolean check = (arr[1] == null ? false : true);

I want to find if arr[1] literally exists or not...not the value in it...
 
Rooks Forgenal
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OH! ok. Wouter Oet has it right. Assuming you have an integer named 'index' try this:
 
prathi suku
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the solution. Acually the array length is not causing the problem .
The problem I am facing is this:

I have a String array arr[], which has values like {"a","b","c"} or {"","",""}
The array length value remains the same
I am using the String split method on the array like arr.split("\"").
So in the first case,arr[] values get stored like
arr[0]=a
arr[1]=,
arr[2]=b
arr[3]=,
arr[4]=c
I am taking the last value in the array and assigning it to a variable.For example the value "c" which is arr[4] needs to be assigned to a variable.This is working fine.

When trying to do the same for the second case where the strings are empty "",I get arrayindexout of bound exception. arr[4] does not seem to exist in this case.
I am checking a condition
if (arr[4].equals("") || arr[4]==null)
but it throws the arrayindexoutof bound exception before reaching here,telling that arr[4] does not exist.
Please advise.
 
W. Joe Smith
Ranch Hand
Posts: 710
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
prathi suku wrote:Thanks for the solution. Acually the array length is not causing the problem .
The problem I am facing is this:

I have a String array arr[], which has values like {"a","b","c"} or {"","",""}
The array length value remains the same
I am using the String split method on the array like arr.split("\"").
So in the first case,arr[] values get stored like
arr[0]=a
arr[1]=,
arr[2]=b
arr[3]=,
arr[4]=c
I am taking the last value in the array and assigning it to a variable.For example the value "c" which is arr[4] needs to be assigned to a variable.This is working fine.

When trying to do the same for the second case where the strings are empty "",I get arrayindexout of bound exception. arr[4] does not seem to exist in this case.
I am checking a condition
if (arr[4].equals("") || arr[4]==null)
but it throws the arrayindexoutof bound exception before reaching here,telling that arr[4] does not exist.
Please advise.


Can you post your code? i'd like to see how you are declaring your arrays.
 
Rooks Forgenal
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well I can see you need to reverse the order of this if statement.

Should be:

Remember that Java will check each boolean left to right. If you do it the original way and arr[4] is in fact null, then the first check will do this. null.equals("") and that does not make sense.

It works like this. For && statements, the left side of the && is checked for FALSE.
For ||, the left side of the || is checked for TRUE.
In either case, if it finds this condition it will either fall through or execute body respectively without ever looking at the next boolean.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12186
34
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
if you're getting an array out of bounds exception, the order of the check won't matter. arr[4] will die whether it's followed by == null or == "".

without checking the documentation, i think split creates a new array only as big as it needs to. So if you try and split an empty string, since there is nothing to split on there is nothing to put into the array. so if you do something like

String myString = "";
String [] myArray = myString.split('/');

your myArray will have Zero elements.

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic