Creating a buy sign and check if line 3 is a number, how?

Discussion in 'Plugin Development' started by EnchantedMiners, May 26, 2014.

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

    EnchantedMiners

    my English sucks ik but please try to understand so I think the title make sense so I want a sign like:
    [Buy] <Done!>
    [Item Name] <I have this one done>
    [Price] <Stuck on this one>
    So what in code look like is:

    Code:java
    1. @EventHandler
    2. public void signs(SignChangeEvent e){
    3. String line1 = e.getLine(0);
    4. String line2 = e.getLine(1);
    5. if(line1.equalsIgnoreCase("[buy]")){
    6. Material items = Material.getMaterial(line2);
    7. Material itemsid = Material.getMaterial(line2);
    8. if(items != null){
    9. int i = Integer.parseInt(e.getLine(3));
    10. if(i > 1 && i < 100000){
    11. e.setLine(0, ChatColor.BLUE + "[Buy]");
    12. e.setLine(1, ChatColor.DARK_BLUE + items.toString());
    13. e.setLine(2, Integer.toString(i));
    14. } else {
    15. e.setLine(0, ChatColor.DARK_RED + "[Buy]");
    16. e.setLine(1, ChatColor.RED + "WRONG PRICE");
    17. }
    18. } else if (itemsid != null){
    19. int i = Integer.parseInt(e.getLine(3));
    20. if(i > 1 && i < 100000){
    21. e.setLine(0, ChatColor.BLUE + "[Buy]");
    22. e.setLine(1, ChatColor.DARK_BLUE + items.toString());
    23. e.setLine(2, Integer.toString(i));
    24. } else {
    25. e.setLine(0, ChatColor.DARK_RED + "[Buy]");
    26. e.setLine(1, ChatColor.RED + "WRONG PRICE");
    27. }
    28. } else {
    29. e.setLine(0, ChatColor.DARK_RED + "[Buy]");
    30. e.setLine(1, ChatColor.RED + "WRONG ITEMNAME");
    31. }
    32. }
    33. }


    Please try youre best :D
     
  2. Offline

    flaaghara

    Integer.parseInt() will throw a NumberFormatException if it isn't passed an int. Just surround your code with a try-catch block and if it catches, you know that line 3 wasn't an int.
     
    Sabersamus likes this.
  3. Offline

    EnchantedMiners

    flaaghara
    I get this error:
    Code:
    [19:29:55] [Server thread/WARN]: java.lang.NumberFormatException: For input string: ""
    [19:29:55] [Server thread/WARN]:    at java.lang.NumberFormatException.forInputString(Unknown Source)
    [19:29:55] [Server thread/WARN]:    at java.lang.Integer.parseInt(Unknown Source)
    [19:29:55] [Server thread/WARN]:    at java.lang.Integer.parseInt(Unknown Source)
    [19:29:55] [Server thread/WARN]:    at me.itzkillaplaysmc.mckilla.listeners.signs(listeners.java:226)
    [19:29:55] [Server thread/WARN]:    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    [19:29:55] [Server thread/WARN]:    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    [19:29:55] [Server thread/WARN]:    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    [19:29:55] [Server thread/WARN]:    at java.lang.reflect.Method.invoke(Unknown Source)
    [19:29:55] [Server thread/WARN]:    at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:292)
    [19:29:55] [Server thread/WARN]:    at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62)
    [19:29:55] [Server thread/WARN]:    at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:501)
    [19:29:55] [Server thread/WARN]:    at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:486)
    [19:29:55] [Server thread/WARN]:    at net.minecraft.server.v1_7_R3.PlayerConnection.a(PlayerConnection.java:1586)
    [19:29:55] [Server thread/WARN]:    at net.minecraft.server.v1_7_R3.PacketPlayInUpdateSign.a(SourceFile:48)
    [19:29:55] [Server thread/WARN]:    at net.minecraft.server.v1_7_R3.PacketPlayInUpdateSign.handle(SourceFile:9)
    [19:29:55] [Server thread/WARN]:    at net.minecraft.server.v1_7_R3.NetworkManager.a(NetworkManager.java:157)
    [19:29:55] [Server thread/WARN]:    at net.minecraft.server.v1_7_R3.ServerConnection.c(SourceFile:134)
    [19:29:55] [Server thread/WARN]:    at net.minecraft.server.v1_7_R3.MinecraftServer.v(MinecraftServer.java:667)
    [19:29:55] [Server thread/WARN]:    at net.minecraft.server.v1_7_R3.DedicatedServer.v(DedicatedServer.java:260)
    [19:29:55] [Server thread/WARN]:    at net.minecraft.server.v1_7_R3.MinecraftServer.u(MinecraftServer.java:558)
    [19:29:55] [Server thread/WARN]:    at net.minecraft.server.v1_7_R3.MinecraftServer.run(MinecraftServer.java:469)
    [19:29:55] [Server thread/WARN]:    at net.minecraft.server.v1_7_R3.ThreadServerApplication.run(SourceFile:628)
    NOTE: Line 226 is were i parseInt(line1);
     
  4. Offline

    flaaghara

  5. Offline

    EnchantedMiners

    flaaghara
    I did surround it with try-catch :/ and it sends the same error...
     
  6. Offline

    flaaghara

    Post updated code please.
     
  7. Offline

    EnchantedMiners

    flaaghara
    Hey for the next one tahg me please so i get notice :p and here is my code
    Code:java
    1. if(line1.equalsIgnoreCase("[buy]")){
    2. Material items = Material.getMaterial(line2);
    3. Material itemsid = Material.getMaterial(line2);
    4. if(items != null){
    5. try {
    6. int i = Integer.parseInt(e.getLine(3));
    7. if(i > 1 && i < 100000){
    8. e.setLine(0, ChatColor.BLUE + "[Buy]");
    9. e.setLine(1, ChatColor.DARK_BLUE + items.toString());
    10. e.setLine(2, Integer.toString(i));
    11. } else {
    12. e.setLine(0, ChatColor.DARK_RED + "[Buy]");
    13. e.setLine(1, ChatColor.RED + "WRONG PRICE");
    14. }
    15. } catch(Exception ev){
    16. ev.printStackTrace();
    17. }
    18. } else if (itemsid != null){
    19. try {
    20. int i = Integer.parseInt(e.getLine(3));
    21. if(i > 1 && i < 100000){
    22. e.setLine(0, ChatColor.BLUE + "[Buy]");
    23. e.setLine(1, ChatColor.DARK_BLUE + items.toString());
    24. e.setLine(2, Integer.toString(i));
    25. } else {
    26. e.setLine(0, ChatColor.DARK_RED + "[Buy]");
    27. e.setLine(1, ChatColor.RED + "WRONG PRICE");
    28. }
    29. } catch (Exception ev){
    30. ev.printStackTrace();
    31. }
    32. } else {
    33. e.setLine(0, ChatColor.DARK_RED + "[Buy]");
    34. e.setLine(1, ChatColor.RED + "WRONG ITEMNAME");
    35. }
    36. }

    Idk if i did it right if i did can you post a new code thanks!
     
  8. Offline

    Garris0n

    1. Never just catch an Exception. Catch the relevant exception, in this case a NumberFormatException.
    2. You're printing the stack trace and then complaining that the stack trace was printed. Remove the line that prints it and handle it some other way.
     
  9. Offline

    EnchantedMiners

    Garris0n

    How would i handle it on another way i don't really work with try/catch alot
     
  10. Offline

    Garris0n

    I don't know what you're doing. Tell the player "Error: Invalid Number" or something and exit the method. I have no idea, it's not my plugin.
     
  11. Offline

    Gater12

    EnchantedMiners
    You use try catch to catch the exception.

    If it throws an exception do something.
     
  12. Offline

    EnchantedMiners

    Gater12
    ok thanks, but if i put a right number why is it still throwing the exception ?

    Gater12 Garris0n flaaghara
    OOpsss my fault this was solved since i posted it i was checking if the line 4 was a number and because its only [buy] [item] [price] [blank] my fault this is solved :D thanks though

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 8, 2016
Thread Status:
Not open for further replies.

Share This Page