Introducing Radical.sh

Forget Code launches a powerful code generator for building API's

Checking Whether The String Contains Number or Not in Java

  1. public class NumberInString {
  2. public static void main(String[] args) {
  3. String[] str = new String[]{"32455", "44.6", "45.isvalid"};
  4. for(int i=0 ; i < str.length ; i ++)
  5. {
  6. if( str[i].indexOf(".") > 0 )
  7. {
  8. try
  9. {
  10. Double.parseDouble(str[i]);
  11. System.out.println(str[i] + " is a valid decimal number");
  12. }
  13. catch(NumberFormatException nme)
  14. {
  15. System.out.println(str[i] + " is not a valid decimal number");
  16. }
  17. }
  18. else
  19. {
  20. try
  21. {
  22. Integer.parseInt(str[i]);
  23. System.out.println(str[i] + " is valid integer number");
  24. }
  25. catch(NumberFormatException nme)
  26. {
  27. System.out.println(str[i] + " is not a valid integer number");
  28. }
  29. }
  30. }
  31. }
  32. }