Home » Java programming language

# How to check if string is number in java?

**Checking string is a number or not**: Here, we are going to learn to **check whether a given string is a number or not in Java?**

Submitted by Preeti Jain, on July 04, 2019

We will check whether string is a number or not – with the help of logic we will solve this problem,

- In the first step, we will take a string variable named str and store any value in it.
- In the second step, We will take a boolean variable named str_numeric which stores Boolean value like true or false. Let us suppose that given string is numeric so that initially boolean variable str_numeric is set to true.
- In the third step we will do one thing in the try block we will convert String variable to Double by using parseDouble() method because initially we are assuming that given the string is number that's why we are converting first.
- If it throws an error (i.e. NumberFormatException), it means given String is not a number and then at the same time boolean variable str_numeric is set to false. Otherwise given string is a number.

**Example:**

public class IsStringNumeric { public static void main(String[] args) { // We have initialized a string variable with double values String str1 = "1248.258"; // We have initialized a Boolean variable and // initially we are assuming that string is a number // so that the value is set to true boolean str_numeric = true; try { // Here we are converting string to double // and why we are taking double because // it is a large data type in numbers and // if we take integer then we can't work // with double values because we can't covert // double to int then, in that case, // we will get an exception so that Boolean variable // is set to false that means we will get wrong results. Double num1 = Double.parseDouble(str1); } // Here it will raise an exception // when given input string is not a number // then the Boolean variable is set to false. catch (NumberFormatException e) { str_numeric = false; } // if will execute when given string is a number if (str_numeric) System.out.println(str1 + " is a number"); // Else will execute when given string is not a number else System.out.println(str1 + " is not a number"); } }

**Output**

D:\Programs>javac IsStringNumeric.java D:\Programs>java IsStringNumeric 1248.258 is a number

TOP Interview Coding Problems/Challenges

- Run-length encoding (find/print frequency of letters in a string)
- Sort an array of 0's, 1's and 2's in linear time complexity
- Checking Anagrams (check whether two string is anagrams or not)
- Relative sorting algorithm
- Finding subarray with given sum
- Find the level in a binary tree with given sum K
- Check whether a Binary Tree is BST (Binary Search Tree) or not
- 1[0]1 Pattern Count
- Capitalize first and last letter of each word in a line
- Print vertical sum of a binary tree
- Print Boundary Sum of a Binary Tree
- Reverse a single linked list
- Greedy Strategy to solve major algorithm problems
- Job sequencing problem
- Root to leaf Path Sum
- Exit Point in a Matrix
- Find length of loop in a linked list
- Toppers of Class
- Print All Nodes that don't have Sibling
- Transform to Sum Tree
- Shortest Source to Destination Path

Comments and Discussions

**Ad:**
Are you a blogger? Join our Blogging forum.