From charlesreid1

No edit summary
No edit summary
Line 1: Line 1:
==Check if a string is numeric==
To check if a String in Java is numeric, build a simple utility function:
To check if a String in Java is numeric, build a simple utility function:


Line 4: Line 6:
public static boolean isNumeric(String str)   
public static boolean isNumeric(String str)   
{   
{   
  try   
    try   
  {   
    {   
    double d = Double.parseDouble(str);   
        double d = Double.parseDouble(str);   
  }   
    }   
  catch(NumberFormatException nfe)   
      catch(NumberFormatException nfe)   
  {   
    {   
    return false;   
        return false;   
  }   
    }   
  return true;   
    return true;   
}
}
</pre>
</pre>

Revision as of 20:32, 18 June 2017

Check if a string is numeric

To check if a String in Java is numeric, build a simple utility function:

public static boolean isNumeric(String str)  
{  
    try  
    {  
        double d = Double.parseDouble(str);  
    }  
      catch(NumberFormatException nfe)  
    {  
        return false;  
    }  
    return true;  
}


Flags





See also: