akdsfjakls
akdsfjakls

Reputation: 208

How to find if an array index Exists?

Basically I need to check if a String contains 2 indexes. Based on my googling I found that I could Either use part[0].length() > 0 || part[0] != null But none happen to help me here.

My code:

String[] parts = datareceived.split("&");

if(!(parts[0].length()>0) && parts[0] == null){
    out.print("String is null");
    return;
}

if(!(parts[1].length()>0) && parts[1] == null){
    out.print("String is null");
    return;
} 

But here in parts[1] i'm getting an exception which says:

java.lang.ArrayIndexOutOfBoundsException: 1 at pack.reg.pack.serv.doPost(serv.java:10) at javax.servlet.http.HttpServlet.service(HttpServlet.java:648) at javax.servlet.http.HttpServlet.service(HttpServlet.java:729)

Thanks in advance!

Upvotes: 1

Views: 9643

Answers (3)

PixelMaster
PixelMaster

Reputation: 954

When you're getting an ArrayIndexOutOfBoundsException when calling parts[1], but not when calling parts[0], we can conclude that parts.length == 1. Hence, since indices start at 0, index 0 contains the whole string, and index 1 in the split array (and up) doesn't exist.

--> Problem found: Your datareceived doesn't contain a &, hence it won't be split anywhere. Solution: Hoping I understood your problem correctly (checking whether a string datareceived contains a value at certain indices), I wrote you a piece of code that should work.

String datareceived = "01234";

int index1 = 2;
int index2 = 5;
if (datareceived.length() > index1) {
    //the string has a value at index1. With the given example, this if-query would be true.
}
if (datareceived.length() > index2) {
    //the string has a value at index2. With the given example, this if-query would be false.
}

Side-note when using .split: I found that, when using String.split("expression"), the "expression" part can contain regex-codes (regular expression). Therefore, if you're using any symbol that is a valid regex expression (such as . or $), it will not work (at least, not necessarily). For example, in regex, . means "any character", which will essentially give you an empty array.
(Note: I'm no regex expert, but "&" doesn't appear to be a valid regular expression)

Example:

String s = "a,b";
String[] strings = s.split(",");
for (String str : strings) {
    System.out.println(str + "_");
}
//will print out "a_b_"

Example (not working as desired):

String s = "a.b";
String[] strings = s.split(".");
//"strings" is an empty array, since "a", ".", and "b" are "any character".

Solution: instead of .split("."), use .split("\\.")

Upvotes: 0

Bhagwati Malav
Bhagwati Malav

Reputation: 3559

It means split returning an array of string and its size is 1. Thats why you are getting java.lang.ArrayIndexOutOfBoundsException and if you want to check whether array size is 2 you can do following

  part.length >= 2
    // write your logic here

Upvotes: 0

user3437460
user3437460

Reputation: 17454

Basically I need to check if a String contains 2 indexes

If you are using split() it returns you an array which you can use .length to check the size of the returned tokens:

if(parts.length >= 2)

But that is not gonna tell if the second index is empty, right?

If you are afraid of getting empty string, you can trim() the String first:

String[] parts = datareceived.trim().split("&");

Upvotes: 2

Related Questions