Solved Check if int contains commas/dots

Discussion in 'Plugin Development' started by DoggyCode™, Jul 22, 2015.

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

    DoggyCode™

    Hey! I'm trying to make this clearchat plugin, where basically one can do "/clearchat <int/lines>"- I've accomplished to check if the argument actually is an integer, but I find no way to actually check if it contains any commas/dots; because you can't really clear 2.5 lines.. This is what I have so far, and it works great, only problem is that I don't know how to check if the argument/int contains commas or not, and if it does, execute some code. Anyways, here's my code so far:

    Code:
    
    //isInt method
        public static boolean isInt(String s) {
            try {
                Integer.parseInt(s);
            } catch (NumberFormatException nfe) {
                return false;
            }
            return true;
        }
    
    //command
            if(cmd.getName().equalsIgnoreCase("clearchat")) {
                if(args.length == 0) {
                    for(int i = 0; i < 100; i++) {
                        Bukkit.broadcastMessage("  ");
                    }
                    Bukkit.broadcastMessage(ChatColor.BLUE + "The chat has been cleared by " + ChatColor.RED + sender.getName());
                } else if(args.length == 1) {
                    String s = args[0];
                    int sInt = Integer.parseInt(args[0]);
                    if(sInt <= 0) {
                        sender.sendMessage(ChatColor.RED + "An integer with the value higher than 0 has to be specified");
                        return true;
                    } else {
                        if(isInt(s)) {
                            for(int i = 0; i < Integer.parseInt(args[0]); i++) {
                                Bukkit.broadcastMessage("  ");
                            }
                            Bukkit.broadcastMessage("§a" + Integer.parseInt(args[0]) + ChatColor.BLUE + " line(s) of the chat has been cleared by " + ChatColor.RED + sender.getName());
                        } else if(!isInt(s)){
                            sender.sendMessage(ChatColor.RED + "An integer has to be specified.");
                            return true;
                        }
                    }
                }
            }
    
    Any help would be greatly appreciated! :)
     
  2. Offline

    Shinxs

    What you could do is making a String from it and then check if that contains a comma's/dots. This is quite an easy solution. But as it is you are parsing an int so there isn't any comma or dot so this isn't really necessary :)
     
  3. Offline

    DoggyCode™

    @Shinxs Like this?:
    Code:
    String s = args[0];
    if(s.contains(",") || s.contains(".")) {
    
    Haha.. yea, sounds good. xD
    I tried this:
    Code:
    int sInt = Integer.parseInt(args[0]);
    if(sInt.contains(...) {
    
    but that didn't work xD Anyways thanks!
     
  4. Offline

    Shinxs

    That should do the job
     
  5. Offline

    DoggyCode™

    @Shinxs thanks! I could have thought of that myself xD Well, people do simple mistakes sometimes.
     
  6. Offline

    Shinxs

    Hahah, quite a simple solution. Sometimes you try to think big and difficult but then the solution is very simple ;)
     
  7. Offline

    DoggyCode™

    Bump. @Shinxs 's method kind of does work, but not in my case apparantly. This is my code:
    Code:
       public static boolean isInt(String s) {
            try {
                Integer.parseInt(s);
            } catch (NumberFormatException nfe) {
                return false;
            }
            return true;
        }
    
    Code:
                } else if(args.length == 1) {
                    String s = args[0];
                    int sInt = Integer.parseInt(args[0]);
                    if(s.contains(",") || s.contains(".")) {
                        sender.sendMessage(ChatColor.RED + "Please specify whole numbers!");
                        return true;
                    } else {
                        if(sInt <= 0) {
                            sender.sendMessage(ChatColor.RED + "An integer with the value higher than 0 has to be specified");
                            return true;
                        } else {
                            if(isInt(s)) {
                                for(int i = 0; i < Integer.parseInt(args[0]); i++) {
                                    Bukkit.broadcastMessage("  ");
                                }
                                Bukkit.broadcastMessage("§a" + Integer.parseInt(args[0]) + ChatColor.BLUE + " line(s) of the chat has been cleared by " + ChatColor.RED + sender.getName());
                            } else if(!isInt(s)){
                                sender.sendMessage(ChatColor.RED + "An integer has to be specified.");
                                return true;
                            }
                        }
                    }
                }
    and I still get this one error. This is my stacktrace:
    Code:
    [19:56:49 INFO]: ShadyMe issued server command: /clearchat 1,4
    [19:56:49 ERROR]: null
    org.bukkit.command.CommandException: Unhandled exception executing command 'clea
    rchat' in plugin Experimental v1
            at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46) ~[Cra
    ftbukkit.jar:git-Bukkit-3fc97ff]
            at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:14
    0) ~[Craftbukkit.jar:git-Bukkit-3fc97ff]
            at org.bukkit.craftbukkit.v1_8_R1.CraftServer.dispatchCommand(CraftServe
    r.java:625) ~[Craftbukkit.jar:git-Bukkit-3fc97ff]
            at net.minecraft.server.v1_8_R1.PlayerConnection.handleCommand(PlayerCon
    nection.java:1058) [Craftbukkit.jar:git-Bukkit-3fc97ff]
            at net.minecraft.server.v1_8_R1.PlayerConnection.a(PlayerConnection.java
    :919) [Craftbukkit.jar:git-Bukkit-3fc97ff]
            at net.minecraft.server.v1_8_R1.PacketPlayInChat.a(SourceFile:37) [Craft
    bukkit.jar:git-Bukkit-3fc97ff]
            at net.minecraft.server.v1_8_R1.PacketPlayInChat.a(SourceFile:9) [Craftb
    ukkit.jar:git-Bukkit-3fc97ff]
            at net.minecraft.server.v1_8_R1.PacketHandleTask.run(SourceFile:13) [Cra
    ftbukkit.jar:git-Bukkit-3fc97ff]
            at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source) [
    ?:1.8.0_45]
            at java.util.concurrent.FutureTask.run(Unknown Source) [?:1.8.0_45]
            at net.minecraft.server.v1_8_R1.MinecraftServer.z(MinecraftServer.java:6
    56) [Craftbukkit.jar:git-Bukkit-3fc97ff]
            at net.minecraft.server.v1_8_R1.DedicatedServer.z(DedicatedServer.java:2
    84) [Craftbukkit.jar:git-Bukkit-3fc97ff]
            at net.minecraft.server.v1_8_R1.MinecraftServer.y(MinecraftServer.java:6
    09) [Craftbukkit.jar:git-Bukkit-3fc97ff]
            at net.minecraft.server.v1_8_R1.MinecraftServer.run(MinecraftServer.java
    :517) [Craftbukkit.jar:git-Bukkit-3fc97ff]
            at java.lang.Thread.run(Unknown Source) [?:1.8.0_45]
    Caused by: java.lang.NumberFormatException: For input string: "1,4"
            at java.lang.NumberFormatException.forInputString(Unknown Source) ~[?:1.
    8.0_45]
            at java.lang.Integer.parseInt(Unknown Source) ~[?:1.8.0_45]
            at java.lang.Integer.parseInt(Unknown Source) ~[?:1.8.0_45]
            at me.pvpdog.experimental.Main.onCommand(Main.java:96) ~[?:?]
            at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44) ~[Cra
    ftbukkit.jar:git-Bukkit-3fc97ff]
            ... 14 more
    
     
  8. Offline

    undeaD_D

    @DoggyCode™ you should surround the Integer.parseInt(args[0]) with try and catch ...

    you also don't need to parse the same string twice ;)

    Better Code (quick notepad writeup) (open)
    Code:
            // does input contain '.' and/or ',' ?
            if(input.contains(",") || input.contains(".")) {
                sender.sendMessage(ChatColor.RED + "Please specify whole numbers!");
                return true;
            }
         
            // is input a valid int ?
            int value;
            try {
                value = Integer.parseInt(input);
            } catch (NumberFormatException ex) {
                sender.sendMessage(ChatColor.RED + "Please specify a valid number!");
                return true;
            }
         
            // is i <= 0 ?
            if(value <= 0) {
                sender.sendMessage(ChatColor.RED + "An integer with the value higher than 0 has to be specified");
                return true;
            }
         
            // exit here ^^
            return true;


    have fun ;)
     
  9. Offline

    DoggyCode™

Thread Status:
Not open for further replies.

Share This Page