|
||
|
|||
|
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.
|
|
|||
|
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.
|
|
||||
|
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]+)");
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());
}
__________________
ASCII a question .. Get an ANSI Please surround your code with [highlight=Java]code goes here[/highlight]. |
|
||||
|
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 |
|
||||
|
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]. |
![]() |
| Thread Tools | |
| Display Modes | |
|
|
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? |