Tuesday, 4 April 2017

Program to check if String contains only digits


Sample Program:


 public class CheckDigitString {  
      public static void main(String[] args) {  
           String str1="abcd";  
           String str2="abc1d";  
           String str3="11234567";  
           //provide regex pattern for digits  
           System.out.println(" String str1 :"+str1.matches("[0-9]+"));  
           System.out.println(" String str2 :"+str2.matches("[0-9]+"));  
           System.out.println(" String str3 :"+str3.matches("[0-9]+"));  
      }  
 }  

Output:


  String str1 :false  
  String str2 :false  
  String str3 :true  

Enjoy Learning.

No comments:

Post a Comment