Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 9 of 9

Thread: need to know if the object is a number or character

  1. #1
    Junior Member
    Join Date
    Oct 2009
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default need to know if the object is a number or character

    I have an array list of different kinds of objects, and need to know if each object is a number or a character (a few specific characters). How do I find out if it is a number or not? I can easily do characters by putting each character in if statements but I don't know how to do the numbers.


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: need to know if the object is a number or character

    Can these numbers be decimal numbers? Or are they guaranteed to be integers? If they are integers, you can just use the Character.isDigit methods. Otherwise, it's easiest to use the Regex class and regular expressions. Also, you can use the Character.isLetter method to see if a certain character is a letter.

  3. #3
    Junior Member
    Join Date
    Oct 2009
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: need to know if the object is a number or character

    It does not have to be an integer. I'm doing something similar to making a calculator. how does the regex class work? I've never used it.

  4. #4
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: need to know if the object is a number or character

    I'd recommend looking through the Sun Regex Tutorials, or any other tutorials dealing with regular expressions to understand how they work. Here's a regex I've used that picks up pretty much every form of numbers (integers, decimals, scientific notation or any power 10 expression)
    Pattern.compile("([0-9]*[.]?[0-9]+[eE]{1}-?[0-9]+)"
                                                  + "|([0-9]+[.]?[0-9]*[eE]{1}-?[0-9]+)" + "|([0-9]*[.]?[0-9]+)"
                                                  + "|([0-9]+)");

    Note: It only picks up non-negative numbers. It can easily be modified to pick up negative numbers, but the calculator I had handled minus signs universally, so there was no need to detect negative numbers.

    To use Java's Regex package, you must first create a regex (like above) using the Patern.compile() factory constructor. This will give you a Pattern object which has a method that builds a Matcher object. This Matcher object is the one you'll use to detect the item in question:

    Pattern myRegex = Pattern.compile("([0-9]*[.]?[0-9]+[eE]{1}-?[0-9]+)"
                                                  + "|([0-9]+[.]?[0-9]*[eE]{1}-?[0-9]+)" + "|([0-9]*[.]?[0-9]+)"
                                                  + "|([0-9]+)");
    Matcher matchThis = myRegex.matcher("123.456e3");
    if (matchThis.find())
    {
         System.out.println("Found a number! It's string representation is this: " + matchThis.group());
    }

    Of course, once you have the pure form of the number, you can put that into the Double.parseDouble() method and get out the value you want.

  5. #5
    Super Moderator Json's Avatar
    Join Date
    Jul 2009
    Location
    Warrington, United Kingdom
    Posts
    1,274
    My Mood
    Happy
    Thanks
    70
    Thanked 156 Times in 152 Posts

    Default Re: need to know if the object is a number or character

    Or you can have a look at [Apache-SVN] Contents of /commons/proper/lang/trunk/src/java/org/apache/commons/lang/math/NumberUtils.java

    Search for a method called isNumber

    For the people out there trying to do things with strings etc, just look into apache commons already, its absolutely brilliant and will cover most of your needs.

    // Json

  6. #6
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: need to know if the object is a number or character

    But then you have to get Apache I prefer to start with just the standard Java API. Besides, learning regular expressions is a good idea anyways.

  7. #7
    Senile Half-Wit Freaky Chris's Avatar
    Join Date
    Mar 2009
    Posts
    834
    My Mood
    Cynical
    Thanks
    7
    Thanked 105 Times in 90 Posts

    Default Re: need to know if the object is a number or character

    If there is a solution without external Libraries use it me says. Unless its stupidly complicated but les be honest....it's only regex

  8. #8
    Super Moderator Json's Avatar
    Join Date
    Jul 2009
    Location
    Warrington, United Kingdom
    Posts
    1,274
    My Mood
    Happy
    Thanks
    70
    Thanked 156 Times in 152 Posts

    Default Re: need to know if the object is a number or character

    It all depends I guess but if the footprint is small enough there's no reason to invent the wheel again

    // Json

  9. #9
    Senile Half-Wit Freaky Chris's Avatar
    Join Date
    Mar 2009
    Posts
    834
    My Mood
    Cynical
    Thanks
    7
    Thanked 105 Times in 90 Posts

    Default Re: need to know if the object is a number or character

    True, but not having the library on other computers annoys the hell out of me lol!

    Chris

Similar Threads

  1. Replies: 5
    Last Post: April 22nd, 2013, 07:27 AM
  2. Vietnamese Character Problem in JSP Report. Chinese and Thai Char OK
    By mrvora in forum JavaServer Pages: JSP & JSTL
    Replies: 7
    Last Post: October 23rd, 2009, 02:35 AM
  3. [SOLVED] Getting Exception " java.util.InputMismatchException" in scanner package
    By luke in forum File I/O & Other I/O Streams
    Replies: 10
    Last Post: May 20th, 2009, 04:55 AM
  4. [SOLVED] How to string a decimal number in Java?
    By Lizard in forum Loops & Control Statements
    Replies: 6
    Last Post: May 14th, 2009, 03:59 PM
  5. How to read character from image area(jpg image)?
    By sundarjothi in forum Java Theory & Questions
    Replies: 5
    Last Post: August 6th, 2008, 02:08 AM