How to check if String is number in java ?

By | June 21, 2020

In this post, We will learn a couple of ways to check if String is number in java?

  1. The First approach uses regular Expression [-+]?\\d*\\.?\\d+ to check whether input string represents number or not.
  2. 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

 Client program CheckStringIsNumberOrNot.java

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.

Leave a Reply

Your email address will not be published.