-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathString10.java
More file actions
45 lines (35 loc) · 1.23 KB
/
String10.java
File metadata and controls
45 lines (35 loc) · 1.23 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
import java.util.*;
public class String10 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
sc.nextLine();
System.out.println("Masked Data:");
for (int i = 1; i <= n; i++) {
String s = sc.nextLine();
String digits = s.replaceAll("[^0-9]", "");
String last4 = digits.length() <= 4 ? digits : digits.substring(digits.length() - 4);
StringBuilder result = new StringBuilder();
int digitCount = 0;
int totalDigits = digits.length();
for (int j = 0; j < s.length(); j++) {
char c = s.charAt(j);
if (Character.isDigit(c)) {
if (digitCount < totalDigits - 4) {
result.append("*");
} else {
result.append(c);
}
digitCount++;
} else {
result.append(c);
}
}
if (totalDigits <= 4) {
result = new StringBuilder(last4);
}
System.out.println(i + ". " + result.toString());
}
sc.close();
}
}