Check if string contains valid number example

  1. /*
  2. Check if string contains valid number example.
  3. This example shows how to check if string contains valid number
  4. or not using parseDouble and parseInteger methods of
  5. Double and Integer wrapper classes.
  6. */
  7.  
  8. public class CheckValidNumberExample {
  9.  
  10. public static void main(String[] args) {
  11.  
  12. String[] str = new String[]{"10.20", "123456", "12.invalid"};
  13.  
  14. for(int i=0 ; i < str.length ; i ++)
  15. {
  16.  
  17. if( str[i].indexOf(".") > 0 )
  18. {
  19.  
  20. try
  21. {
  22. /*
  23. * To check if the number is valid decimal number, use
  24. * double parseDouble(String str) method of
  25. * Double wrapper class.
  26. *
  27. * This method throws NumberFormatException if the
  28. * argument string is not a valid decimal number.
  29. */
  30. Double.parseDouble(str[i]);
  31. System.out.println(str[i] + " is a valid decimal number");
  32. }
  33. catch(NumberFormatException nme)
  34. {
  35. System.out.println(str[i] + " is not a valid decimal number");
  36. }
  37.  
  38. }
  39. else
  40. {
  41. try
  42. {
  43. /*
  44. * To check if the number is valid integer number, use
  45. * int parseInt(String str) method of
  46. * Integer wrapper class.
  47. *
  48. * This method throws NumberFormatException if the
  49. * argument string is not a valid integer number.
  50. */
  51.  
  52. Integer.parseInt(str[i]);
  53. System.out.println(str[i] + " is valid integer number");
  54. }
  55. catch(NumberFormatException nme)
  56. {
  57. System.out.println(str[i] + " is not a valid integer number");
  58. }
  59. }
  60. }
  61.  
  62. }
  63. }
  64.  
  65. /*
  66. Output would be
  67. 10.20 is a valid decimal number
  68. 123456 is valid integer number
  69. 12.invalid is not a valid decimal number
  70. */

Visit Java Example Forums to request Java examples or ask Java questions!
OR
Try out our new Java Search Engine




Suggested Reading

Oracle Magazine
Contains technology strategy articles, sample code, tips, Oracle and partner news, how to articles for developers and DBAs, and more.
Cost: FREE

View/Subscribe
NASA Tech Briefs
Features exclusive reports of innovations developed by NASA and its industry partners/contractors that can be applied to develop new/improved products and solve engineering or manufacturing problems.
Cost: FREE

View/Subscribe
FierceBiotech IT
Is a free, easy to read weekly email service that brings must read biotech IT news to senior biotech, pharma, and IT executives.
Cost: FREE

View/Subscribe
Simply SQL
Simply SQL is a practical step-by-step guide to writing SQL.
Cost: FREE

View/Subscribe
Simply JavaScript
Packed with full-color examples, Simply JavaScript is all you need to start programming in JavaScript the right way.
Cost: FREE

View/Subscribe
PCMag.com's What's New Now
Lance Ulanoff, Editor in Chief of the PC Magazine Network, brings you this twice-weekly roundup of the latest top tech stories, the best new product reviews, plus special offers from Ziff Davis and its partners.
Cost: FREE

View/Subscribe

Could not find what you are looking for? Search Java Examples




Feel Tired? Read Jokes & Inspirational Stories, Play Games!