Testing for Args 1 to 1000000 in 1 line of code

Discussion in 'Plugin Development' started by JarFile, Jan 23, 2015.

Thread Status:
Not open for further replies.
  1. Offline

    JarFile

    Heres my code
    Code:
                if(label.equalsIgnoreCase("alert"))
                {
                    if(args.length == 0)
                    {
                        p.playSound(p.getLocation(), Sound.CAT_HISS, 1, 2);
                        p.sendMessage(ChatColor.DARK_RED + "Invalid Syntax </alert [message]>");
                    }
                    if(args.length == 1)
                    {
                        Bukkit.broadcastMessage("§8§l[§4§lAlert§8§l] §c§l" + args[0]);
                    }
                    if(args.length == 2)
                    {
                        Bukkit.broadcastMessage("§8§l[§4§lAlert§8§l] §c§l" + args[0] + " " + args[1]);
                    }
                    if(args.length == 3)
                    {
                        Bukkit.broadcastMessage("§8§l[§4§lAlert§8§l] §c§l" + args[0] + " " + args[1] + " " + args[2]);
                    }
                    if(args.length == 4)
                    {
                        Bukkit.broadcastMessage("§8§l[§4§lAlert§8§l] §c§l" + args[0] + " " + args[1] + " " + args[2] + " " + args[3]);
                    }
                }
    
    I'm pretty sure there is an easier way than this. Is there anyway I can test for
    if(args.length == 1 to 1000000)
    {

    }
    how would I do that in just one line.
     
  2. Offline

    adam753

    Use a for-loop to get every string in the args array and add them together. I'm not going to give you the code but you can probably find many examples of this if you search.
     
  3. Offline

    teej107

    1. Create a StringBuilder
    2. for loop through the array
    3. append the strings in the array together
    4. Get the string from the StringBuilder
    All this can be done in ~4 lines of code
     
  4. Offline

    1Rogue

    Or 1 line of code:

    Code:java
    1. Bukkit.broadcastMessage(ChatColor.translateAlternateColorCodes('&', "&8&l[&4&lAlert&8&l] &c&l ") + StringUtils.join(args, " "));
     
Thread Status:
Not open for further replies.

Share This Page