Search code examples
javaargumentsisset

Java - Check If Argument Is Empty or Is Set?


I'm very new to Java, and I'm trying to see if an argument being set is empty, and if so, to send a message to them, then kill the script. It's not appearing to work though, because I leave the argument empty, and I still will just simply not get my message, but it appears it's returning false.

public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args){
    Player player = (Player) sender;

    if(commandLabel.equalsIgnoreCase("register")){
        if(args[0].length() == 0){
            player.sendMessage(ChatColor.GOLD + "You Did Not Set A Password! (Ex: /register mysupersecretpassword)");
            return false;
        }
    }
    return false;

Final Solution

if(args.length == 0){

Not

if(args[0].length() == 0){

Solution

  • Your problem is your checking the 0th position of args length.

    if(args[0].length() == 0){
    

    needs to be

    if(args.length == 0){
    

    In fact if your checking the 0th position of args length you will probably get a array out of bounds error.