私はユーザーからの入力を分割しようとしています !stats username
そのコマンドは正常に動作しますが、ユーザーがユーザー名 !stats
私のチェッカーが失敗してクラッシュする Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1
if (!Config.str.split("!stats")[1].isEmpty()) {
}
回答:
回答№1は2配列のサイズをチェックする必要があります。usernameの場合、配列にはインデックス0の要素が1つしかありません。文字列が空になっているかどうかをチェックして、最後に "!stats"
String[] split = Config.str.split("!stats");
if(split.length > 1 && !split[1].trim().isEmpty()) {
//do something
}
回答№2の場合は0
str.split("!stats");
エラーを出さない
あなたの条件は間違っているので例外を出しています
boolean condition = Config.str.split("!stats").length<=0 ? false : true;
if (condition) {
//your code
}
多分あなたはこのように試してみるべきです