Getting Integers from Lores

Discussion in 'Plugin Development' started by xxPatterson, Nov 29, 2015.

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

    xxPatterson

    [​IMG]
    I have been trying to get the int for both "Success Rate" and "Destroy Rate"
    I will give the example of what I have so far it it is currently picking up the Success Rate as
    "Rate 91%" amd thats causing a NumberFormatException.
    Please comment if you have any idea on how to fix this, thanks.
    Code:
        public static int getDestroyRate(ItemStack i) {
            if(i != null && i.hasItemMeta() && i.getItemMeta().hasLore()) {
                List<String> lore = i.getItemMeta().getLore();
                for(String s : lore) {
                    s = ChatColor.stripColor(s);
                    if(s.contains("Destroy Rate")) {
                        int value = Integer.parseInt(s.substring(s.indexOf(" ") + 1));
                        return value;
                    }
                }
            }
    
            return 0;
        }
        public static int getSuccessRate(ItemStack i) {
            if(i != null && i.hasItemMeta() && i.getItemMeta().hasLore()) {
                List<String> lore = i.getItemMeta().getLore();
                for(String s : lore) {
                    s = ChatColor.stripColor(s);
                    if(s.contains("Success Rate")) {
                        int value = Integer.parseInt(s.substring(s.indexOf(" ") + 1));
                        return value;
                    }
                }
            }
    
            return 0;
        }
    
     
  2. Offline

    Xerox262

    Most likely due to the percentage sign at the end, have you tried getting a substring and excluding the % sign at the end?
    Example:
    int value = Integer.parseInt(s.substring(s.indexOf(" ") + 1, s.indexOf("%")));

    Edit: Ninja'd @mcdorli

    Or
    int value = Integer.parseInt(s.replaceAll("[^0-9]"));
     
    Last edited: Nov 29, 2015
    tommyhoogstra likes this.
  3. Offline

    mcdorli

    Get the string
    Remove Success Rate and %
    Integer.parseInt(<what you got>)
     
  4. Offline

    xxPatterson

    After getting the string how do I remove Success Rate and %?
     
  5. Offline

    mcdorli

    String#replace("Success Rate", "");
    String#replace("%", "");
     
  6. Offline

    Scimiguy

    EDIT: both mcdorli's method and mine will work, but mine will work on any text in your lore, as long as the last word contains your number

    String#split(" ")[2].replace("%", "")
     
    Last edited: Nov 29, 2015
  7. Offline

    mcdorli

    There is no method like String#split(Int i), only String#split(char[]), and split returns an array of strings
    you could do String#split(" ")[2].replace("%", "")
     
  8. Offline

    Scimiguy

    That's what i meant -.- I'm on a phone lol

    I'll edit it into mine, once this damn website lets me click anything
     
  9. Offline

    mcdorli

    I feel your pain. I'm often from a phone too.
     
  10. Offline

    xxPatterson

    @mcdorli
    Code:
    public static int getSuccessRate(ItemStack i) {
            if(i != null && i.hasItemMeta() && i.getItemMeta().hasLore()) {
                List<String> lore = i.getItemMeta().getLore();
                for(String s : lore) {
                    s = ChatColor.stripColor(s);
                    if(s.contains("Success Rate")) {
                        s.replace("Success Rate", "");
                        s.replace("%", "");
                        int value = Integer.parseInt(s);
                        return value;
                    }
                }
            }
    
            return 0;
        }
    
    }
    This is returning "Success Rate 91%" and still catching the "NumberFormatException"

    @mcdorli
    Code:
    public static int getSuccessRate(ItemStack i) {
            if(i != null && i.hasItemMeta() && i.getItemMeta().hasLore()) {
                List<String> lore = i.getItemMeta().getLore();
                for(String s : lore) {
                    s = ChatColor.stripColor(s);
                    if(s.contains("Success Rate")) {
                        s.replace("Success Rate", "");
                        s.replace("%", "");
                        int value = Integer.parseInt(s);
                        return value;
                    }
                }
            }
    
            return 0;
        }
    
    }
    This is returning "Success Rate 91%" and still catching the "NumberFormatException"

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Nov 29, 2015
  11. Offline

    Mrs. bwfctower

    @xxPatterson You have to set s to the new value. e.g. s = s.replace(...)
     
  12. Offline

    Nanoripper

    you must do it like this:

    s = s.replace(...);

    also instead of "Success Rate" use "Success Rate " because there is also a space character after success rate which will cause exception while parseInt, if not removed.
     
Thread Status:
Not open for further replies.

Share This Page