Integer help

Discussion in 'Plugin Development' started by XFarwar, Feb 5, 2015.

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

    XFarwar

    Hi, i'm in troblue with this code:
    Code:
    int duration = Integer.parseInt(args[3]);
                            int secondo = duration *20;
                            int power = Integer.parseInt(args[4]);
                            if(secondo < 1000000){
                                if(power < 255){
                                    if(effetto != null){
                                        p.addPotionEffect(new PotionEffect(effetto, secondo, power));
                                        int pw = power+1;
                                        p.sendMessage(success + "§bYou added §3" + args[2] + " §7(§ePower:" + pw  + "§7) §bto §3" + args[1] + " §bfor§3 " + duration + " §bseconds.");
                                        t.sendMessage("§3" + p.getName() + " §badded to you §3" + args[2] + " §7(§ePower:" + pw + "§7) §bfor§3 " + duration + " §bseconds.");
                                    }else{
                                        invalidEffect(p, args);
                                    }
                                }else{
                                    p.sendMessage("§cThe power is too hight. Max: 255");
                                }
                            }else{
                                p.sendMessage("§cThe duration is too hight. Max: 100000");
                            }
    When I use an character not equals to an number the plugin gives me an internal error (obviously). How can I send a message to a player if the args[3] (the duration) and the args[4](the power) containts character that are not numbers?
     
  2. Offline

    Lolmewn

    try{
    int num = Integer.parseInt(args[3]);
    }catch(NumberFormatException ex){
    //send message that it's not a number
    }
     
  3. Offline

    LordDarthBob

    @XFarwar
    You can use this code to safely get an integer from a string (Just include a null check):
    Code:
       public java.lang.Integer getInt(String s) {
            int i;
            try {
               i = Integer.parseInt(s);
            } catch (NumberFormatException nfe) {
               return null;
            }
           return i;
    }
    
    EDIT: I been ninja'd
    EDIT 2: Made code better based on @teej107 's suggestion
     
    Last edited: Feb 6, 2015
  4. Offline

    XFarwar

    @Lolmewn Can you insert it in my code?
    It gives me an error:
    Code:
    if(t != null){
                          PotionEffectType effetto = PotionEffectType.getByName(args[2]);
                            try{
                                int duration = Integer.parseInt(args[3]);
                                }catch(NumberFormatException ex){
                                //send message that it's not a number
                                }
                            int secondo = duration *20;
    
                            if(secondo < 1000000){
                                if(effetto != null){
                                    p.sendMessage("§7/effects add <player> <effect> <duration> §e<power>");
                                }else{
                                    invalidEffect(p, args);
                                }
                            }else{
                                p.sendMessage("§cThe duration is too hight. Max: 100000");
                            }
     
    Last edited: Feb 5, 2015
  5. Offline

    teej107

    No person (with decency) is going to write your code for you. If you don't know where to put that method and/or use it, then I suggest you continue learning Java first as a knowledge of it is required to make plugins.
     
  6. Offline

    XFarwar

    Sorry, you're code doesn't work, infact, the int secondo can't use 'second'.
     
  7. Offline

    Darkpicasa

    It seems like you are copying this code from somewhere. I know of zero developers that would name variables "secondo" and "effetto."

    Also, @LordDarthBob is completely right. That's all you need. If you don't know how to use functions like that, learn Java before wasting your time struggling through Bukkit.
     
  8. Offline

    teej107

    Slight edit to his code: Have it return an Integer instead or null if the parsing failed. You won't need to call any method twice just to get an int from a String and all you have to do to get the Integer is to check if the return value is null or not.
     
    LordDarthBob likes this.
  9. Offline

    Minesuchtiiii

    @Darkpicasa
    I think he is using "effetto" and "secondo" because he is italian... no problem bro.

    @XFarwar
    Put this infront of your code:

    Code:
    if(isInt(args[3])) {
    if(isInt(args[4])) {
    
    //Do code..
    
    } else {
    p.sendMessage("No number found, please insert a number!");
    }
    
    } else {
    p.sendMessage("No number found, please insert a number!");
    }
     
Thread Status:
Not open for further replies.

Share This Page