In this post, We will learn a couple of ways to check if String is number in java?
- The First approach uses regular Expression [-+]?\\d*\\.?\\d+ to check whether input string represents number or not.
- The second approach uses method parseDouble(InputString) from Double class if the input String successfully parse into a number then input String is number otherwise Double.parseDouble(InputString) will throw NumberFormatException then input string is not a number
CheckStringIsNumberOrNotUtil.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
package com.kkjavatutorials.client; public class CheckStringIsNumberOrNotUtil { public static boolean isNumber1(String inputString) { return inputString != null && inputString.matches("[-+]?\\d*\\.?\\d+"); } public static boolean isNumber2(String inputString) { if (inputString == null || inputString.length() == 0) { return false; } try { Double.parseDouble(inputString); return true; } catch (NumberFormatException e) { return false; } } } |
Client program CheckStringIsNumberOrNot.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
package com.kkjavatutorials.client; public class CheckStringIsNumberOrNot { public static void main(String[] args) { System.out.println("---Testing Method CheckStringIsNumberOrNotUtil.isNumber1(inputStr)---"); System.out.println("123 is Number:"+CheckStringIsNumberOrNotUtil.isNumber1("123")); System.out.println("45.92 is Number: "+CheckStringIsNumberOrNotUtil.isNumber1("45.92")); System.out.println("xy12 is Number: "+CheckStringIsNumberOrNotUtil.isNumber1("xy12")); System.out.println("-701 is Number: "+CheckStringIsNumberOrNotUtil.isNumber1("-701")); System.out.println("---Testing Method CheckStringIsNumberOrNotUtil.isNumber2(inputStr)---"); System.out.println("123 is Number:"+CheckStringIsNumberOrNotUtil.isNumber2("123")); System.out.println("45.92 is Number: "+CheckStringIsNumberOrNotUtil.isNumber2("45.92")); System.out.println("xy12 is Number: "+CheckStringIsNumberOrNotUtil.isNumber2("xy12")); System.out.println("-701 is Number: "+CheckStringIsNumberOrNotUtil.isNumber2("-701")); } } |
Output of this program:
—Testing Method CheckStringIsNumberOrNotUtil.isNumber1(inputStr)—
123 is Number:true
45.92 is Number: true
xy12 is Number: false
-701 is Number: true
—Testing Method CheckStringIsNumberOrNotUtil.isNumber2(inputStr)—
123 is Number:true
45.92 is Number: true
xy12 is Number: false
-701 is Number: true
You May Also Like:
How to convert number to words in java
How to swap two numbers with or without temporary variable in java
Java Program to Swap two numbers using Bitwise XOR Operator?
How to reverse a number in Java
How to check Armstrong number java program
Java program to find factorial of a number
Java Program to Calculate the Power of a Number
Check whether a number is prime or not
Java Program to displaying prime numbers
Fibonacci series using iterative and recursive approach java program
That’s all about How to check if String is number in java?
If you have any feedback or suggestion please feel free to drop in below comment box.