Go Back   Java Programming Forums > Java Standard Edition Programming Help > Java SE APIs


Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 26-11-2009, 01:44 AM
Junior Member
 

Join Date: Oct 2009
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
karneson05 is on a distinguished road
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.



Reply With Quote Share this thread on Facebook
Sponsored Links
Java Training from DevelopIntelligence
  #2 (permalink)  
Old 26-11-2009, 02:02 AM
helloworld922's Avatar
Super Moderator
 

Join Date: Jun 2009
Posts: 1,215
Thanks: 5
Thanked 258 Times in 234 Posts
helloworld922 will become famous soon enoughhelloworld922 will become famous soon enoughhelloworld922 will become famous soon enough
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.
__________________
ASCII a question .. Get an ANSI

Please surround your code with [highlight=Java]code goes here[/highlight].
Reply With Quote
  #3 (permalink)  
Old 26-11-2009, 02:17 AM
Junior Member
 

Join Date: Oct 2009
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
karneson05 is on a distinguished road
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.
Reply With Quote
  #4 (permalink)  
Old 26-11-2009, 04:45 AM
helloworld922's Avatar
Super Moderator
 

Join Date: Jun 2009
Posts: 1,215
Thanks: 5
Thanked 258 Times in 234 Posts
helloworld922 will become famous soon enoughhelloworld922 will become famous soon enoughhelloworld922 will become famous soon enough
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)
Java Code
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:

Java Code
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.
__________________
ASCII a question .. Get an ANSI

Please surround your code with [highlight=Java]code goes here[/highlight].
Reply With Quote
  #5 (permalink)  
Old 26-11-2009, 07:37 AM
Json's Avatar
Super Moderator
 

Join Date: Jul 2009
Location: Manchester, United Kingdom
Posts: 1,157
Thanks: 54
Thanked 136 Times in 132 Posts
Json will become famous soon enoughJson will become famous soon enoughJson will become famous soon enough

I'm feeling Happy
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
Reply With Quote
  #6 (permalink)  
Old 26-11-2009, 03:58 PM
helloworld922's Avatar
Super Moderator
 

Join Date: Jun 2009
Posts: 1,215
Thanks: 5
Thanked 258 Times in 234 Posts
helloworld922 will become famous soon enoughhelloworld922 will become famous soon enoughhelloworld922 will become famous soon enough
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.
__________________
ASCII a question .. Get an ANSI

Please surround your code with [highlight=Java]code goes here[/highlight].
Reply With Quote
  #7 (permalink)  
Old 26-11-2009, 11:22 PM
Freaky Chris's Avatar
Senile Half-Wit
 

Join Date: Mar 2009
Location: UK
Posts: 621
Thanks: 2
Thanked 64 Times in 60 Posts
Freaky Chris will become famous soon enoughFreaky Chris will become famous soon enoughFreaky Chris will become famous soon enough

I'm feeling Cheerful
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
__________________
chris[at]javaprogrammingforums[dot]com

University of Wales, Bangor
Reply With Quote
  #8 (permalink)  
Old 27-11-2009, 11:03 AM
Json's Avatar
Super Moderator
 

Join Date: Jul 2009
Location: Manchester, United Kingdom
Posts: 1,157
Thanks: 54
Thanked 136 Times in 132 Posts
Json will become famous soon enoughJson will become famous soon enoughJson will become famous soon enough

I'm feeling Happy
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
Reply With Quote
  #9 (permalink)  
Old 27-11-2009, 03:24 PM
Freaky Chris's Avatar
Senile Half-Wit
 

Join Date: Mar 2009
Location: UK
Posts: 621
Thanks: 2
Thanked 64 Times in 60 Posts
Freaky Chris will become famous soon enoughFreaky Chris will become famous soon enoughFreaky Chris will become famous soon enough

I'm feeling Cheerful
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
__________________
chris[at]javaprogrammingforums[dot]com

University of Wales, Bangor
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



Similar Threads
Thread Thread Starter Forum Replies Last Post
Vietnamese Character Problem in JSP Report. Chinese and Thai Char OK mrvora JavaServer Pages: JSP & JSTL 7 23-10-2009 07:35 AM
Using recursion to find the number of occurences of a character in a string sean1604 Algorithms & Recursion 3 22-10-2009 11:39 PM
[SOLVED] checking if it is a character? luke File I/O & Other I/O Streams 10 20-05-2009 09:55 AM
[SOLVED] how to string a number? Lizard Loops & Control Statements 6 14-05-2009 08:59 PM
Read character from Image area sundarjothi Java Theory & Questions 5 06-08-2008 07:08 AM


100 most searched terms
Search Cloud
2 dimensional arraylist java 2d arraylist java actionlistener actionlistener in java addactionlistener addactionlistener java convert double to integer java double format java double to integer in java double to integer java drag en drop programmeren java eclipse shortcut keys exception in thread "awt-eventqueue-0" java.lang.outofmemoryerror: java heap space exception in thread "main" java.lang.nullpointerexception exception in thread "main" java.lang.outofmemoryerror: java heap space format double in java format double java get mouse position java java 2d arraylist java actionlistener java double format java double formatting java double to int java double to integer java format double java forum java forums java get mouse position java list to map java mouse position java programming forum java programming forums java programming practice problems java send keystrokes to another application java two dimensional arraylist java.io.ioexception: premature eof java.lang.classformaterror: truncated class file java.lang.outofmemoryerror: java heap space java.util.arraylist jbutton action jbutton actionlistener jtextarea font jtextfield font size jxl.read.biff.biffexception: unable to recognize ole stream programming mutators and generics smack api two dimensional arraylist two dimensional arraylist java unable to sendviapost to url what is the smallest positive number that is evenly divisible by all of the numbers from 1 to 20?

All times are GMT. The time now is 01:59 AM.
Powered by vBulletin® Copyright ©2000-2009, Jelsoft Enterprises Ltd.