-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathString1.java
More file actions
50 lines (42 loc) · 1.47 KB
/
String1.java
File metadata and controls
50 lines (42 loc) · 1.47 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
48
49
50
import java.util.*;
public class String1 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
sc.nextLine();
int validCount = 0;
int invalidCount = 0;
System.out.println("Email Validation Results:");
for (int i = 1; i <= n; i++) {
String email = sc.nextLine();
boolean isValid = true;
int atIndex = email.indexOf('@');
int lastAtIndex = email.lastIndexOf('@');
if (atIndex == -1 || atIndex != lastAtIndex) {
isValid = false;
}
else if (atIndex == 0 || atIndex == email.length() - 1) {
isValid = false;
}
else if (email.startsWith(".") || email.endsWith(".")) {
isValid = false;
}
else {
int dotIndex = email.indexOf('.', atIndex);
if (dotIndex == -1 || dotIndex == email.length() - 1) {
isValid = false;
}
}
if (isValid) {
System.out.println(i + ". " + email + " - Valid");
validCount++;
} else {
System.out.println(i + ". " + email + " - Invalid");
invalidCount++;
}
}
System.out.println("Total Valid: " + validCount);
System.out.println("Total Invalid: " + invalidCount);
sc.close();
}
}