1// Java program for the above approach
2// contains only digits
3class GFG {
4
5 // Function to check if a string
6 // contains only digits
7 public static boolean
8 onlyDigits(String str, int n)
9 {
10 // Traverse the string from
11 // start to end
12 for (int i = 0; i < n; i++) {
13
14 // Check if character is
15 // digit from 0-9
16 // then return true
17 // else false
18 if (str.charAt(i) >= '0'
19 && str.charAt(i) <= '9') {
20 return true;
21 }
22 else {
23 return false;
24 }
25 }
26 return false;
27 }
28
29 // Driver Code
30 public static void main(String args[])
31 {
32 // Given string str
33 String str = "1234";
34 int len = str.length();
35
36 // Function Call
37 System.out.println(onlyDigits(str, len));
38 }
39}
40