How to check if String contains only numeric in Java

0
7777

Here we are going to see a code sample which would help to determine if a string contains only numeric digits or not. We can also find the number of digits it contains. So we will also look at the different ways to find numeric digits in String as well as count of digits in the String. To do so, Java provides regular expressions using java.util.regex.Pattern and java.util.regex.Matchter class.

Complete Code Sample to check if String contains only numeric digits

import java.util.regex.Matcher;
import java.util.regex.Pattern;


/*
* How to test if the input String contains only digits or not
*/
public class GoyalsBitStringTesting {

    //This method would apply two checks on the received string :
    //1: It should contain all numeric values only
    //2. Number of numerics should be exact 7
    public static boolean areAllNumericWayOne(String id){
        Pattern p = Pattern.compile("\\d{7}");
        Matcher m = p.matcher(id);
        boolean b = m.matches();
        return b;
    }
    
    //This method would apply two checks on the received string :
    //1: It should contain all numeric values only
    //2. Number of numerics can be one or more
    public static boolean areAllNumericWayTwo(String id){
        Pattern p = Pattern.compile("[0-9]+");
        Matcher m = p.matcher(id);
        boolean b = m.matches();
        return b;
    }
    
    //This method would apply two checks on the string & is an alternate approach to the above one:
    //1: It should contain all numeric values only
    //2. Number of numerics can be one or more
    public static boolean areAllNumericWayThree(String id){
        String regex = "[0-9]+";
        boolean b = id.matches(regex);
        return b;
    }
    
    public static void main(String args[]){
        
        System.out.println("1234567 contains 7 digits only : " + areAllNumericWayOne("1234567"));
        System.out.println("12345678 contains 7 digits only : " + areAllNumericWayOne("12345678"));
        
        System.out.println("1 contains Numeric only : " + areAllNumericWayTwo("1"));
        System.out.println("123456 contains Numeric only : " + areAllNumericWayTwo("123456"));
        
        System.out.println("12345678 contains Numeric only : " + areAllNumericWayThree("12345678"));
        System.out.println("123456789 contains Numeric only : " + areAllNumericWayThree("123456789"));
        
        System.out.println("ABC123 contains Numeric digits only : " + areAllNumericWayOne("ABC123"));
        System.out.println("ABC123 contains Numeric digits only : " + areAllNumericWayTwo("ABC123"));
        System.out.println("ABC123 contains Numeric digits only: " + areAllNumericWayThree("ABC123"));
    }
}

Output for the above program is :

1234567 contains 7 digits only : true
12345678 contains 7 digits only : false
1 contains Numeric only : true
123456 contains Numeric only : true
12345678 contains Numeric only : true
123456789 contains Numeric only : true
ABC123 contains Numeric digits only : false
ABC123 contains Numeric digits only : false
ABC123 contains Numeric digits only: false

 

The code is self explanatory and I have given the comments in code itself to make more clear. One to note that you can change the number of digits in regular expression based on your need. In the above example it is 7 but you can put any number you want and it would look for those number of digits only in your string.

Previous articleTop 10 Eclipse Short cut keys
Next articleCheck if String contains Alpha Chars in Java
I have spent almost 10 years playing around Java and related technologies. I love to write on different topics and would be very much willing to hear back your feedback/suggestions on them. This site is a medium to share my knowledge with the Java folks and grow further. My other interests include traveling, driving, swimming and dance. But yes, my web site has become my passion over the time :) I live in Scotland and travel to India often, my roots being there.

NO COMMENTS

LEAVE A REPLY