Parse Double Error

Discussion in 'Spigot Plugin Development' started by BlahBerrys, Jun 8, 2017.

  1. Stacktrace:
    Code (Text):

    [19:28:15 ERROR]: Could not pass event InventoryClickEvent to GSEnchants v1.0
            at org.bukkit.plugin.EventExecutor$1.execute( ~[pa
            at co.aikar.timings.TimedEventExecutor.execute(
    8) ~[paperspigot.jar:git-Paper-1104]
            at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.jav
    a:62) ~[paperspigot.jar:git-Paper-1104]
            at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.j
    ava:517) ~[paperspigot.jar:git-Paper-1104]
            at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.j
    ava:502) ~[paperspigot.jar:git-Paper-1104]
            at net.minecraft.server.v1_11_R1.PlayerConnection.a(PlayerConnection.jav
    a:1898) ~[paperspigot.jar:git-Paper-1104]
            at net.minecraft.server.v1_11_R1.PacketPlayInWindowClick.a(SourceFile:33
    ) ~[paperspigot.jar:git-Paper-1104]
            at net.minecraft.server.v1_11_R1.PacketPlayInWindowClick.a(SourceFile:10
    ) ~[paperspigot.jar:git-Paper-1104]
            at net.minecraft.server.v1_11_R1.PlayerConnectionUtils.lambda$ensureMain
    Thread$0( ~[paperspigot.jar:git-Paper-1104]
            at java.util.concurrent.Executors$ Source) [
            at Source) [?:1.8.0_131]
            at net.minecraft.server.v1_11_R1.SystemUtils.a(SourceFile:46) [paperspig
            at net.minecraft.server.v1_11_R1.MinecraftServer.D(
    839) [paperspigot.jar:git-Paper-1104]
            at net.minecraft.server.v1_11_R1.DedicatedServer.D(
    403) [paperspigot.jar:git-Paper-1104]
            at net.minecraft.server.v1_11_R1.MinecraftServer.C(
    763) [paperspigot.jar:git-Paper-1104]
    a:661) [paperspigot.jar:git-Paper-1104]
            at Source) [?:1.8.0_131]
    Caused by: java.lang.NumberFormatException: For input string: "100]"
            at sun.misc.FloatingDecimal.readJavaFormatString(Unknown Source) ~[?:1.8
            at sun.misc.FloatingDecimal.parseDouble(Unknown Source) ~[?:1.8.0_131]
            at java.lang.Double.parseDouble(Unknown Source) ~[?:1.8.0_131]
            at com.whoisericballard.gsenchants.GUIHandler.onClickSelection(GUIHandle ~[?:?]
            at com.destroystokyo.paper.event.executor.asm.generated.GeneratedEventEx
    ecutor1.execute(Unknown Source) ~[?:?]
            at org.bukkit.plugin.EventExecutor$1.execute( ~[pa
            ... 16 more
    Code (Text):

        @EventHandler(priority = EventPriority.NORMAL, ignoreCancelled = true)
        public void onClickSelection(InventoryClickEvent event) {
            if (!(event.getWhoClicked() instanceof Player))
            final Player player = (Player) event.getWhoClicked();

            if (!event.getInventory().getTitle().contains("Enchantments") && !event.getInventory().getTitle().contains(" "))
            if (event.getCurrentItem() != null && event.getCurrentItem().hasItemMeta() && event.getCurrentItem().getItemMeta().getLore() != null) {

                String xp = ChatColor.stripColor(event.getCurrentItem().getItemMeta().getLore().toString().split(":")[1].replaceAll(" ", ""));
                double requiredXP  = Double.parseDouble(xp); //this is line 124

                if ((int) player.getExp() < requiredXP)
                    player.sendMessage("You do not have enough XP!");
                else {
                    String name = ChatColor.stripColor(event.getCurrentItem().getItemMeta().getDisplayName());
                    player.getInventory().addItem(Enchantment.getEnchantPiece(event.getCurrentItem().getItemMeta().getDisplayName(), Category.getByName(name), Rarity.getbyName(name)));
                    player.sendMessage("You have purchased a " + name + ChatColor.GRAY + " for " + ChatColor.GOLD + requiredXP + ChatColor.GRAY + "XP!");
                    player.setExp(player.getExp() - (float) requiredXP);

  2. It says the problem in the error:
    Code (Text):
    Caused by: java.lang.NumberFormatException: For input string: "100]"
    "100]" is not a valid number, gotta grab it without the ]
    • Like Like x 1
  3. >.>

    I've been programming for five hours straight. I should probably take a break now, ha.
  4. use String#replace as String#replaceAll is for regex usage.
    • Agree Agree x 2
  5. What you could do is do a replaceAll call that replaces anything that isn't a number. Like this:
    Code (Text):
    String xpFiltered = xp.replaceAll("[^0-9]+", "");
    Quick explanation of that
    replaceAll replaces things in a String with a regex pattern
    [^0-9]+ matches anything that isn't a digit between 0 and 9, and that just replaces them with empty space.
    At that point, you could use a quick if statement to check if the filtered string is empty, and tell the user if it is, otherwise parse the double and do whatever
  6. If you want to use regex (which you probably shouldn't in this case, since indexOf/substring is faster), at least use pattern matching, it's less error prone (f.e. including colour codes or random numbers in your result, or eliminating decimal points)