-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCountDigits.java
More file actions
38 lines (33 loc) · 1.68 KB
/
Copy pathCountDigits.java
File metadata and controls
38 lines (33 loc) · 1.68 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
package practice;
import java.util.Optional;
public class CountDigits {
public static int countDigits(Optional<Integer> num) {
return num.map(n -> String.valueOf(Math.abs(n)).length()).orElse(0);
}
public static int countDigitsWithNull(Integer num) {
return Optional.ofNullable(num)
.map(n -> String.valueOf(Math.abs(n)).length())
.orElse(0);
}
// Method to count the number of digits in a given number string
public static int countDigits(String numStr) {
return Optional.ofNullable(numStr)
.filter(s -> !s.isEmpty() && s.matches("-?\\d+")) // Validate number format // Only allow valid integer numbers
//.map(s -> s.replace("-", "")) // Remove negative sign if present
//.map(String::length)
//.orElse(0);
.map(s -> String.valueOf(Math.abs(Integer.parseInt(s))).length())
.orElse(0);
}
public static void main(String[] args) {
System.out.println(countDigits(Optional.of(12345))); // Output: 5
System.out.println(countDigits(Optional.of(-9876))); // Output: 4
System.out.println(countDigits(Optional.of(0))); // Output: 1
System.out.println(countDigits(Optional.empty())); // Output: 0
System.out.println(countDigits("")); // Output: 0
System.out.println(countDigits("12345")); // Output: 5
System.out.println(countDigits("-98-76")); // Output: 4
System.out.println(countDigitsWithNull(0)); // Output: 1
System.out.println(countDigitsWithNull(null)); // Output: 0
}
}