Parsing argument list specifying a player to be added I want to start off by saying sorry, I wasn't able to search for this problem as I couldn't exactly describe it myself.

I have a series of if-statements to account for each scenario. As you can see in the mammoth block of code below, I want arguments to come in any order. How, if possible, can I shorten this?

if(args[1].startsWith("u:") &amp;&amp; args[2].startsWith("r:") &amp;&amp; args[3].startsWith("l:")){ // u / r / l
    Player p = player.getServer().getPlayer(args[1].split(":")[1]);
    String r = args[2].split(":")[1];
    String l = args[3].split(":")[1];
    Config.addPlayer(p, r, l, player);
    return true;
}else if(args[1].startsWith("r:") &amp;&amp; args[2].startsWith("l:") &amp;&amp; args[3].startsWith("u:")){ // r / l / u
    Player p = player.getServer().getPlayer(args[3].split(":")[1]);
    String r = args[1].split(":")[1];
    String l = args[2].split(":")[1];
    Config.addPlayer(p, r, l, player);
    return true;
}else if(args[1].startsWith("l:") &amp;&amp; args[2].startsWith("u:") &amp;&amp; args[3].startsWith("r:")){ // l / u / r
    Player p = player.getServer().getPlayer(args[2].split(":")[1]);
    String r = args[3].split(":")[1];
    String l = args[1].split(":")[1];
    Config.addPlayer(p, r, l, player);
    return true;
}else if(args[1].startsWith("u") &amp;&amp; args[2].startsWith("l:") &amp;&amp; args[3].startsWith("r:")){ // u / l / r
    Player p = player.getServer().getPlayer(args[1].split(":")[1]);
    String r = args[3].split(":")[1];
    String l = args[2].split(":")[1];
    Config.addPlayer(p, r, l, player);
    return true;
}else if(args[1].startsWith("l:") &amp;&amp; args[2].startsWith("r:") &amp;&amp; args[3].startsWith("u:")){ // l / r / u
    Player p = player.getServer().getPlayer(args[2].split(":")[1]);
    String r = args[2].split(":")[1];
    String l = args[1].split(":")[1];
    Config.addPlayer(p, r, l, player);
    return true;
}else if(args[1].startsWith("r:") &amp;&amp; args[2].startsWith("u:") &amp;&amp; args[3].startsWith("l:")){ // r / u / l
    Player p = player.getServer().getPlayer(args[2].split(":")[1]);
    String r = args[1].split(":")[1];
    String l = args[3].split(":")[1];
    Config.addPlayer(p, r, l, player);
    return true;
}
First of all, don't ever write code like this... you know it can be done better, so don't waste your time.

Moreover, you can be sure, you'll make some errors there, e.g., this looks wrong:

Player p = player.getServer().getPlayer(args[2].split(":")[1]);
String r = args[2].split(":")[1];


as you're using args[2] twice.



It's actually very trivial:

String[] inputs = {args[1], args[2], args[3]);
Arrays.sort(inputs);
if (inputs[0].startsWith("l:") &amp;&amp; inputs[1].startsWith("r:") &amp;&amp; inputs[2].startsWith("u:")) {
    Player p = player.getServer().getPlayer(inputs[2].split(":")[1]);
    String r = inputs[1].split(":")[1];
    String l = inputs[0].split(":")[1];
    Config.addPlayer(p, r, l, player);
    return true;
} else {
    return false;
}


A possibly even simpler alternative would be to go through the args and always fill the appropriate data slot (p, r, and l). After processing all three args, all slots must be filled, otherwise the input is wrong.



There's much more to say... e.g., your repetitive

args[2].split(":")[1]


should be replaced by a method. You also most probably don't want to allow args like l:12:34:567 (or you don't want to lose the part after the second colon), so don't use split.

Actually, I guess, you need some command line parser... there are at least 10 such things in Java freely available.