samedi 17 septembre 2016

Java split if it find text after command

I am trying from hours how to do this so I will ask here for help. I run a IRC bot and trying to do something, when user is sending command to the bot it needs to see if it can spit the input from the user if there is one. Example: !help lag 2 3 The code works for this just fine but if the user only send Example: !help Its crashing with Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1

Here is the code for that part:

String command = str.split("!")[1];
command = command.split("\\s+", 2)[0];
String username_fix = java.net.URLEncoder.encode(username, "UTF-8").replace("_", "%20");
String combined = "";
String fix = str.split(command)[1];
fix = fix.trim();
String arrayString[] = fix.split("\\s+");
for (int j = 0; j < arrayString.length; j++) {
combined += arrayString[j] + "|";
}

Error is coming from String fix = str.split(command)[1];

Aucun commentaire:

Enregistrer un commentaire