Calculator Help

Discussion in 'Plugin Development' started by Zach_1919, May 2, 2013.

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

    Zach_1919

    Hi, I am making a calculator plugin. I know it's been done a million times but I am doing it as a practice. I made it so that you can add up to four integers. I wasn't sure what would happen if I put in less then four args, so I put some line saying that if that arg isn't put into the command, then set it to 0 so it doesn't affect the sum. It doesn't work though....

    Code:
    Code:
    package me.Zach_1919.Calc;
     
    import org.bukkit.ChatColor;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.event.Listener;
    import org.bukkit.plugin.java.JavaPlugin;
     
    public class Calc extends JavaPlugin implements Listener
    {
        public void onEnable()
        {
            getServer().getPluginManager().registerEvents(this,this);
        }
     
        public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args)
        {
            if(commandLabel.equalsIgnoreCase("add"))
            {
                if(args[2].equals(null))
                {
                    args[2].replace(null, "0");
                }
                else if(args[3].equals(null))
                {
                    args[3].replace(null, "0");
                }
                int add1 = Integer.parseInt(args[0]);
                int add2 = Integer.parseInt(args[1]);
                int add3 = Integer.parseInt(args[2]);
                int add4 = Integer.parseInt(args[3]);
                String sum = String.valueOf(add1+add2+add3+add4);
                sender.sendMessage(ChatColor.GOLD + "Answer = " + sum);
            }
            return false;
         
        }
    }
    
     
  2. Offline

    daviga404

    If the user specifies less than 4 arguments, the args[3], args[2], etc. will not be null, they will just not be in the array, so there will only be args[0] and args[1] for example. A much easier way to do this would be to loop through the arguments and add it to an integer. For example:
    Code:java
    1.  
    2. int i = 0;
    3. for(String s : args){
    4. i += Integer.parseInt(s);
    5. }
    6.  

    Which will add all of the arguments to one integer which you can then give to the user.
     
  3. Offline

    macguy8

    daviga404
    You'd want to surround that with a try-catch block also in case the argument they entered isn't a integer.
     
  4. Offline

    marwzoor

    macguy8

    I usually make a method that checks if it is a number:
    Code:java
    1.  
    2. public boolean isNumber(String args)
    3. {
    4. boolean parsable=true;
    5. try
    6. {
    7. Integer.parseInt(args);
    8. }
    9. {
    10. parsable=false;
    11. }
    12. return parsable;
    13. }
    14.  

    So if you can parse it and it is a number it will return true, else if it contains characters and is not parsable it will return false.
     
  5. Offline

    macguy8

    marwzoor
    True, but in the shown code, it'd be easier to just use a try-catch instead of adding another method to the OP's plugin
     
  6. Offline

    Zach_1919

    daviga404 That's great and all, but I want to know why mine isn't working.
     
  7. Offline

    marwzoor

    Code:java
    1.  
    2. package me.Zach_1919.Calc;
    3.  
    4. import org.bukkit.ChatColor;
    5. import org.bukkit.command.Command;
    6. import org.bukkit.command.CommandSender;
    7. import org.bukkit.event.Listener;
    8. import org.bukkit.plugin.java.JavaPlugin;
    9.  
    10. public class Calc extends JavaPlugin implements Listener
    11. {
    12. public void onEnable()
    13. {
    14. getServer().getPluginManager().registerEvents(this,this);
    15. }
    16.  
    17. public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args)
    18. {
    19. if(commandLabel.equalsIgnoreCase("add"))
    20. {
    21. if(args[2].equals(null))
    22. {
    23. args[2].replace(null, "0");
    24. }
    25. else if(args[3].equals(null))
    26. {
    27. args[3].replace(null, "0");
    28. }
    29. int add1 = Integer.parseInt(args[0]);
    30. int add2 = Integer.parseInt(args[1]);
    31. int add3 = Integer.parseInt(args[2]);
    32. int add4 = Integer.parseInt(args[3]);
    33. int sum = add1+add2+add3+add4;
    34. sender.sendMessage(ChatColor.GOLD + "Answer = " + sum);
    35. }
    36. return false;
    37.  
    38. }
    39. }
    40.  
    41.  


    Try that instead!
     
  8. Offline

    Tirelessly

    Thankfully the first answer told you why it wasn't working.

     
  9. Offline

    gomeow

  10. Offline

    Zach_1919

    marwzoor I tried that and it didn't work so I just made it into a two variable adder and did the same thing with subtraction, multiplication, and division. Thanks though
     
Thread Status:
Not open for further replies.

Share This Page