1

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!

asked Dec 14, 2016 at 13:56
3
  • 2
    use parts.length (length is not a function). Commented Dec 14, 2016 at 13:57
  • @KevinEsche But that is not gonna tell if the second index is empty, right? Commented Dec 14, 2016 at 13:59
  • @akdsfjakls No, this will tell you how many elements there are, but it doesn't know if they're empty strings. Commented Dec 14, 2016 at 14:00

3 Answers 3

2

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("&");
answered Dec 14, 2016 at 13:59
0

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
answered Dec 14, 2016 at 14:25
0

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("\\.")

answered Dec 14, 2016 at 15:24

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.