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





Comments and Discussions

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




Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.