javaargumentsminecraftargs

How do I remove an argument from a String without removing any potential duplicates?


I am currently working on a Java program that takes the arguments of a person's input (I figured that part out) and then adds it to a string to then send it to another person. However, I'm stuck with this one part, where the first argument is not to be included in the message. I originally thought of if (!arg.equals(args[0])){}, but in the case where the first argument is mentioned again in the message, it also removes that from the message. How do I make it remove the first argument without getting rid of any others exactly like it?

My code right now:

if (args.length<=0){
            sender.sendMessage(ChatColor.RED + "No player specified.");
        } else {
            if (args.length==1){
                sender.sendMessage(ChatColor.RED + "No message specified");
            } else {
                String message = "";
                for (String arg : args){
                    if (!arg.equals(args[0])){

                    }
                }
            }
        }

Solution

  • You can start the for-loop from index one. That way the first argument will not be included at all.

    if (args.length <= 0) {
        sender.sendMessage(ChatColor.RED + "No player specified.");
    } else {
        if (args.length == 1) {
            sender.sendMessage(ChatColor.RED + "No message specified");
        } else {
            StringBuilder message = new StringBuilder();
            for (int i = 1; i < args.length; i++) {
                message.append(args[i]);
            }
        }
    }