Check if command arg contains invalid character

Discussion in 'Plugin Development' started by goldencreeper, Sep 19, 2012.

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

    goldencreeper

    How would i check if a command argument contains only numbers?

    Example:
    if a player types /cbb 456 it will do the rest of the code.

    if a player types /ccb @34gdg#$b8 it will send the player a message saying "Invalid Characters"

    I only want the command to continue to read the rest of the code if it only contains numbers

    Thx
     
  2. Offline

    Eistee²


    if(arg[x].matches(".*[1-9].*") == true)
    {
    //your code
    }
    should work , else goggle after "java regex" :)
     
  3. Offline

    goldencreeper

    wouldnt that try to see if it matches anything because of the .*?
     
  4. Offline

    gregthegeek

    If you're trying to get it as a number value anyway, it might be easier to do this:
    Code:
    try {
        int n = Integer.parseInt(arg[x]);
        //your code
    } catch (NumberFormatException e) {
        //not a valid number
    }
     
  5. Offline

    Eistee²

    No , it would only check if in the String Contains only the numbers any where :)


    yeah sure that would work too but I dont like try/catch :eek:
     
  6. Offline

    goldencreeper


    if(arg[x].matches(".*[1-9].*") == true) that does work but it doesnt detect specials Chars like: !@#$%^&*() ...etc
     
  7. Offline

    Eistee²

    then do it with try catch ^^ I dont know which was the operator for this :p
     
  8. Offline

    desht

    Why on earth not? It's the right way to do it.
     
Thread Status:
Not open for further replies.

Share This Page