Solved String to Long parsing: NumberFormatException!

Discussion in 'Plugin Development' started by Blockhead7360, Sep 18, 2016.

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

    Blockhead7360

    Hello! I am having a problem:

    I keep getting a NumberFormatException when parsing a String to a Long, but I can't see why it's not working.

    Here is where it is parsing:
    Code:
    String toParse = e.getInventory().getTitle().replaceFirst(ChatColor.DARK_AQUA + "" + ChatColor.BOLD + "[MTS] " + ChatColor.GRAY + "", "");
                    long presId = Long.parseLong(toParse);

    Here is where the inventory's title is defined. I posted the method header thingy too so you can see that the "id" is a long.
    Code:
        public static void results(Player player, long id, List<String> res){
                Inventory x = Bukkit.getServer().createInventory(null, 54, ChatColor.DARK_AQUA + "" + ChatColor.BOLD + "[MTS] " + ChatColor.GRAY + "" + id);
            
    This is what it is saying in the console:
    Code:
    Caused by: java.lang.NumberFormatException: For input string: "§3§l[MTS] §738"
        at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65) ~[?:1.8.0_77]
    
    It says that it is parsing the entire title, even though I am using replaceFirst.

    Can anyone help?
     
  2. Offline

    Zombie_Striker

    @Blockhead7360
    Do tell me, what number includes paragraph symbols?

    You can only have numbers in a string you want to parse out. Remove all paragraph symbols, letters, and brackets from the string before parsing it.
     
    Blockhead7360 likes this.
  3. Offline

    Blockhead7360

    @Zombie_Striker Did I not? Look at the first method I posted. I replaced the first part of the title with nothing so just the number would be left. If I am doing something wrong with that, please tell me.
     
  4. Offline

    Zombie_Striker

    Try doing the following:
    1. Split the string at "§" (note: Make sure your plugin is UTF compatible. Basically, if this throws an error on exporting, just make sure it is exported using UTF-8)
    2. After splitting the string, get the very last string and use .substring(1) to remove that "7" from the endbit
    3. Parse this new string.
     
  5. Offline

    Blockhead7360

    @Zombie_Striker looks like it worked! Thanks!

    I don't know why the replaceFirst thing didn't work, as it worked every other time I did it. But oh well.

    Here is what I did:
    Code:
                    String toParse = e.getInventory().getTitle().split("§")[3].substring(1);
    
     
Thread Status:
Not open for further replies.

Share This Page