Get variables from try catch block

Discussion in 'Spigot Plugin Development' started by zozidalom, May 28, 2016.

Thread Status:
Not open for further replies.
  1. So I am createing a command that uses vault. I am checking in a try catch block if one of the args is a number or not... But how can I get the variable out of the try catch ? http://pastebin.com/gd66VUzJ
    I tried with Double value = null; but It didn't work...
    Please help me!
     
  2. Can you paste the code in your post, My firewall is blocking pastebin.com
     
  3. Code (Text):
    public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {
            if(cmd.getName().equalsIgnoreCase("ribal")) {
                if (sender instanceof Player) {
                Player player = (Player) sender;
                if(player.hasPermission("risingempire.economy")) {
                        if (args.length == 0) {
                            sender.sendMessage(ChatColor.RED + "Helyes használat: /ribal <give|set> <összeg> <játékos>");
                        }
                        if (args.length == 1) {
                            sender.sendMessage(ChatColor.RED + "Helyes használat: /ribal <give|set> <összeg> <játékos>");
                        }
                        if (args.length == 2) {
                            sender.sendMessage(ChatColor.RED + "Helyes használat: /ribal <give|set> <összeg> <játékos>");
                        }
                        if (args.length == 3) {
                            if (args[0] != null && args[1] != null) {
                                Player targetplayer = player.getServer().getPlayer(args[2]); //Target player
                                    String penz = args[1]; //Balance S
                                try {
                                    Double value = Double.parseDouble(penz); //Balance D
                                } catch(NumberFormatException ex) {
                                    player.sendMessage(ChatColor.RED + "A megadott összeg érvénytelen...");
                                }
                                    Double cb = main.eco.getBalance(targetplayer); //Cureent balance
                                    if (args[0].equalsIgnoreCase("give")) {
                                            if(targetplayer.isOnline())
                                            main.eco.depositPlayer(targetplayer, value);
                                        }
                                    if (args[0].equalsIgnoreCase("set")) {
                                            if(targetplayer.isOnline())
                                            main.eco.withdrawPlayer(targetplayer, cb);
                                            main.eco.depositPlayer(targetplayer, value);
                                    }
                                }
                            }
                        }
                }
            }
            return true;
        }
     
  4. Make a variable in your class, And not in the try catch block
     
  5. 1. Use double and not Double
    2. Define the variable outside of the try catch block and rather set it inside it.
     
  6. You also able to put the whole function into the try block. That will make sure if it's not a double the method will stop here.
     
  7. Code (Text):
    String test = "Failed!";
    try {
        test = "Worked!";
    }catch(Exception e){

    }
     
  8. Thank you I soloved it
     
  9. Mark your thread as solved ;)
     
Thread Status:
Not open for further replies.