0

I am trying to split the input from user for example !stats username That command works fine but when user don't write username just !stats my checkers fail and crash Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1

if (!Config.str.split("!stats")[1].isEmpty()) {
}
2
  • Save the result of split() into a named array and check it's length before accessing particular elements. Commented Apr 22, 2016 at 20:07
  • Its crashing with the same error Commented Apr 22, 2016 at 20:11

2 Answers 2

2

You need to check size of array, if there is no username then Array will have only one element with index 0. You can also check if string is empty after trim to check if command had only space at the end "!stats "

String[] split = Config.str.split("!stats");
if(split.length > 1 && !split[1].trim().isEmpty()) {
   //do something
}
Sign up to request clarification or add additional context in comments.

Comments

0

str.split("!stats"); doesnt give errors

your condition is wrong it is giving out exceptions

 boolean condition = Config.str.split("!stats").length<=0 ? false : true;

            if (condition) {
                //your code 
            }

maybe you should try it this way

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.