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.
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.
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.
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)
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:
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.
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
Re: need to know if the object is a number or character
But then you have to get Apache :P I prefer to start with just the standard Java API. Besides, learning regular expressions is a good idea anyways.
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
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
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