Random Guy
Random Guy

Reputation: 15

Java split parameter from command

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()) {
}

Upvotes: 0

Views: 41

Answers (2)

Michał
Michał

Reputation: 641

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
}

Upvotes: 2

Priyamal
Priyamal

Reputation: 2969

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

Upvotes: 0

Related Questions