How To Determine If A String Represents An Integer ?

Discussion in 'Java' started by MrBill, Aug 26, 2003.

  1. MrBill

    MrBill Guest

    Hi Experts,
    I have a String of two to four characters in length. I would like to
    determine if it is an integer (each character is in the set 0:9). I could
    check each character individually but there must be an easier (and faster
    way). Any suggestions?
    Thanks,
    Bill
     
    MrBill, Aug 26, 2003
    #1
    1. Advertising

  2. MrBill:

    >I have a String of two to four characters in length. I would like to
    >determine if it is an integer (each character is in the set 0:9). I could
    >check each character individually but there must be an easier (and faster
    >way). Any suggestions?


    public static boolean isValidNumber(String s) {
    try {
    int i = Integer.parseInt(s);
    return s.length() > 1 && s.length() < 5;
    }
    catch (NumberFormatException nfe) {
    return false;
    }
    }

    Not necessarily faster than anything "handmade", but probably more
    readable.

    Regards,
    Marco
    --
    Please reply in the newsgroup, not by email!
    Java programming tips: http://jiu.sourceforge.net/javatips.html
    Other Java pages: http://www.geocities.com/marcoschmidt.geo/java.html
     
    Marco Schmidt, Aug 26, 2003
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Roberto López

    What represents NT AUTHORITY/SYSTEM User??

    Roberto López, Jul 30, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    2,085
    Roberto López
    Jul 30, 2003
  2. MarionEll
    Replies:
    0
    Views:
    449
    MarionEll
    Sep 30, 2003
  3. robert maas, see http://tinyurl.com/uh3t

    Requesting advice how to clean up C code for validating string represents integer

    robert maas, see http://tinyurl.com/uh3t, Feb 11, 2007, in forum: C Programming
    Replies:
    232
    Views:
    3,196
    Mark T
    Apr 16, 2007
  4. Randy Kramer
    Replies:
    12
    Views:
    407
    Robert Klemme
    Oct 25, 2007
  5. Davy
    Replies:
    9
    Views:
    262
    Ted Zlatanov
    Aug 14, 2006
Loading...

Share This Page