Skip to content

Commit c0dd57e

Browse files
authored
Create extra_examples
1 parent 1d63986 commit c0dd57e

File tree

1 file changed

+129
-0
lines changed

1 file changed

+129
-0
lines changed

Diff for: extra_examples

+129
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
import java.util.Scanner;
2+
public class String_Practice_Examples {
3+
// extra examples on Java string and array 2D
4+
public static void main(String[] args) {
5+
// TODO: check if an email is valid or not
6+
Scanner scanner = new Scanner(System.in);
7+
while (true) {
8+
System.out.print("Enter an email (or type 'exit' to quit): ");
9+
String email = scanner.nextLine();
10+
if (email.equalsIgnoreCase("exit")) {
11+
System.out.println("Goodbye!");
12+
break;
13+
}
14+
try {
15+
checkEmail(email); // Validate
16+
String masked = maskEmail(email);
17+
System.out.println("Original: " + email);
18+
System.out.println("Masked: " + masked);
19+
} catch (IllegalArgumentException e) {
20+
System.out.println("Error: " + e.getMessage());
21+
}
22+
}
23+
scanner.close();
24+
25+
// TODO: word and letter counter
26+
String text = "Java is powerful! 123";
27+
int wordCount = countWords(text);
28+
int letterCount = countLetters(text);
29+
System.out.println("Total words: " + wordCount);
30+
System.out.println("Total letters: " + letterCount);
31+
32+
// TODO: search word in 2d array rows or columns
33+
char[][] grid = {
34+
{'J', 'A', 'V', 'A'},
35+
{'P', 'Y', 'T', 'H'},
36+
{'C', 'P', 'H', 'P'},
37+
{'R', 'U', 'B', 'Y'}
38+
};
39+
String word = "JAVA";
40+
boolean found = findWordInGrid(grid, word);
41+
if (!found) {
42+
System.out.println("Word \"" + word + "\" not found in grid.");
43+
}
44+
45+
}// end of main method
46+
47+
// TODO: check email validity mask the email method
48+
public static void checkEmail(String email) {
49+
if (email == null || email.trim().isEmpty()) {
50+
throw new IllegalArgumentException("Email cannot be empty.");
51+
}
52+
email = email.trim();
53+
if (!email.contains("@") || !email.contains(".") ||
54+
email.indexOf("@") > email.lastIndexOf(".") ||
55+
email.startsWith("@") || email.endsWith(".") ||
56+
email.length() < 5) {
57+
throw new IllegalArgumentException("Invalid email format.");
58+
}
59+
}
60+
public static String maskEmail(String email) {
61+
email = email.trim();
62+
int atIndex = email.indexOf('@');
63+
if (atIndex <= 1) {
64+
return "***" + email.substring(atIndex);
65+
}
66+
char first = email.charAt(0);
67+
char last = email.charAt(atIndex - 1);
68+
String domain = email.substring(atIndex);
69+
return first + "***" + last + domain;
70+
}
71+
72+
// TODO: word and letter counter methods
73+
public static int countWords(String text) {
74+
int count = 0;
75+
boolean lastWasSpace = true;
76+
for (int i = 0; i < text.length(); i++) {
77+
char ch = text.charAt(i);
78+
if (Character.isLetterOrDigit(ch)) {
79+
if (lastWasSpace) {
80+
count++;
81+
lastWasSpace = false;
82+
}
83+
}
84+
else {
85+
lastWasSpace = true;
86+
}
87+
}
88+
return count;
89+
}
90+
public static int countLetters(String text) {
91+
int count = 0;
92+
for (int i = 0; i < text.length(); i++) {
93+
char ch = text.charAt(i);
94+
if (Character.isLetter(ch)) {
95+
count++;
96+
}
97+
}
98+
return count;
99+
}
100+
101+
// TODO: search 2d array for a word method
102+
public static boolean findWordInGrid(char[][] grid, String word) {
103+
int rows = grid.length;
104+
int cols = grid[0].length;
105+
// Check each row
106+
for (int i = 0; i < rows; i++) {
107+
String row = "";
108+
for (int j = 0; j < cols; j++) {
109+
row += grid[i][j];
110+
}
111+
if (row.contains(word)) {
112+
System.out.println("Word \"" + word + "\" found in row " + i);
113+
return true;
114+
}
115+
}
116+
// Check each column
117+
for (int j = 0; j < cols; j++) {
118+
String col = "";
119+
for (int i = 0; i < rows; i++) {
120+
col += grid[i][j];
121+
}
122+
if (col.contains(word)) {
123+
System.out.println("Word \"" + word + "\" found in column " + j);
124+
return true;
125+
}
126+
}
127+
return false;
128+
}
129+
}

0 commit comments

Comments
 (0)