aspose file tools*
The moose likes Beginning Java and the fly likes arrayindexoutofbound exception Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "arrayindexoutofbound exception" Watch "arrayindexoutofbound exception" New topic
Author

arrayindexoutofbound exception

prathi suku
Greenhorn

Joined: Sep 14, 2009
Posts: 7
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

Joined: Oct 25, 2008
Posts: 2700

arr.length??


"Any fool can write code that a computer can understand. Good programmers write code that humans can understand." --- Martin Fowler
Please correct my English.
Rooks Forgenal
Ranch Hand

Joined: Jun 05, 2009
Posts: 82
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

Joined: Oct 25, 2008
Posts: 2700

But he is asking about the index not the object at that index.
Rooks Forgenal
Ranch Hand

Joined: Jun 05, 2009
Posts: 82
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

Joined: Sep 14, 2009
Posts: 7
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

Joined: Jun 05, 2009
Posts: 82
OH! ok. Wouter Oet has it right. Assuming you have an integer named 'index' try this:
prathi suku
Greenhorn

Joined: Sep 14, 2009
Posts: 7
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

Joined: Feb 10, 2009
Posts: 710
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.


SCJA
When I die, I want people to look at me and say "Yeah, he might have been crazy, but that was one zarkin frood that knew where his towel was."
Rooks Forgenal
Ranch Hand

Joined: Jun 05, 2009
Posts: 82
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

Joined: Oct 02, 2003
Posts: 11320
    
  16

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.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: arrayindexoutofbound exception