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
    org.bukkit.event.EventException
            at org.bukkit.plugin.EventExecutor$1.execute(EventExecutor.java:46) ~[pa
    perspigot.jar:git-Paper-1104]
            at co.aikar.timings.TimedEventExecutor.execute(TimedEventExecutor.java:7
    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(PlayerConnectionUtils.java:14) ~[paperspigot.jar:git-Paper-1104]
            at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source) [
    ?:1.8.0_131]
            at java.util.concurrent.FutureTask.run(Unknown Source) [?:1.8.0_131]
            at net.minecraft.server.v1_11_R1.SystemUtils.a(SourceFile:46) [paperspig
    ot.jar:git-Paper-1104]
            at net.minecraft.server.v1_11_R1.MinecraftServer.D(MinecraftServer.java:
    839) [paperspigot.jar:git-Paper-1104]
            at net.minecraft.server.v1_11_R1.DedicatedServer.D(DedicatedServer.java:
    403) [paperspigot.jar:git-Paper-1104]
            at net.minecraft.server.v1_11_R1.MinecraftServer.C(MinecraftServer.java:
    763) [paperspigot.jar:git-Paper-1104]
            at net.minecraft.server.v1_11_R1.MinecraftServer.run(MinecraftServer.jav
    a:661) [paperspigot.jar:git-Paper-1104]
            at java.lang.Thread.run(Unknown Source) [?:1.8.0_131]
    Caused by: java.lang.NumberFormatException: For input string: "100]"
            at sun.misc.FloatingDecimal.readJavaFormatString(Unknown Source) ~[?:1.8
    .0_131]
            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
    r.java:124) ~[?:?]
            at com.destroystokyo.paper.event.executor.asm.generated.GeneratedEventEx
    ecutor1.execute(Unknown Source) ~[?:?]
            at org.bukkit.plugin.EventExecutor$1.execute(EventExecutor.java:44) ~[pa
    perspigot.jar:git-Paper-1104]
            ... 16 more
     
    CODE:
    Code (Text):

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

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


                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);
                }
                player.closeInventory();
            }
        }

     
     
  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)