-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathIsStringContainsOnlyDigits.java
More file actions
47 lines (41 loc) · 1 KB
/
IsStringContainsOnlyDigits.java
File metadata and controls
47 lines (41 loc) · 1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package com.javamultiplex.string;
import java.util.Scanner;
/**
*
* @author Rohit Agarwal
* @category String Problems
* @problem How to check whether string contains only digits?
*
*/
public class IsStringContainsOnlyDigits {
public static void main(String[] args) {
Scanner input = null;
try {
input = new Scanner(System.in);
System.out.println("Enter String : ");
String string = input.next();
int length = string.length();
int count = 0;
for (int i = 0; i < length; i++) {
/**
* For checking whether particular character is digit or not we
* can use isDigit(char c) method of Character class.
*/
if (Character.isDigit(string.charAt(i))) {
count++;
} else {
break;
}
}
if (count == length) {
System.out.println("String contains only digits.");
} else {
System.out.println("String not contains only digits.");
}
} finally {
if (input != null) {
input.close();
}
}
}
}