STRINGS

1. Program to Check if a String contains only digits?

            class Main
            {
                static boolean hasDigitsOnly(String str){
                    for(int i = 0; i < str.length(); i++){
                        char ch = str.charAt(i);
                        if(ch < '0' || ch > '9'){
                            return false;
                        }
                    }
                    return true;
                }
                public static void main(String[] args){
                    System.out.println("String Value : 389294");
                    System.out.println((hasDigitsOnly("389294")) ? "String Contain Only Digits" : "String Does not have digits only");
            
                    System.out.println("String Value : 393aje");
                    System.out.println((hasDigitsOnly("393aje")) ? "String Contain Only Digits" : "String Does not have digits only");
                }
            }
        

OUTPUT

            String Value : 389294
            String Contain Only Digits
            String Value : 393aje
            String Does not have digits only