Thread: String to INT
View Single Post
Old 03-20-2011, 02:44 PM   PM User | #3
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,650
Thanks: 4
Thanked 2,451 Times in 2,420 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
pattern matching will help you to tell if it is a number, but still won't cast it so you'll still need the parse if you want to treat it as a number.
Just try/catch it instead, its much simpler. Integer.parseint will throw a NumberFormatException if it can't be parsed:
PHP Code:
try
{
   
Integer x Integer.valueOf(S1);
   
System.out.println("The int is: " x);
}
catch (
NumberFormatException ex)
{
    
System.out.println(S1 " is not a number.");

__________________
As of PHP 5.5, the MySQL library has been officially deprecated. It is recommended to move to either MySQLi or PDO libraries for your mysql connectivity. See here for help choosing which interface you prefer: http://php.net/manual/en/mysqlinfo.api.choosing.php
Fou-Lu is offline   Reply With Quote