-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathL3.java
More file actions
57 lines (42 loc) · 1.36 KB
/
L3.java
File metadata and controls
57 lines (42 loc) · 1.36 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
51
52
53
54
55
56
57
import java.util.ArrayList;
class L3 {
public static int countKeywords(String keywords) {
if (keywords.isEmpty()){
return 0;
}
int Length = 0;
String[] List = keywords.split(";");
ArrayList <String> words = new ArrayList<String>();
for (int i = 0; i < List.length; i++){
words.add(List[i].trim());
}
for (int i = 0; i < words.size(); i++){
if (!words.get(i).isEmpty()){
Length++;
}
}
return Length;
}
public static void main(String[] args) {
// Test 1
String test1 = "";
int result = countKeywords(test1);
System.out.println("Test 1 result: " + result); // expect 0
// Test 2
String test2 = "fruit; vegetables; drinks";
result = countKeywords(test2);
System.out.println("Test 2 result: " + result); // expect 3
// Test 3
String test3 = "flat white; latte; cappuccino; cafe au lait; mocha";
result = countKeywords(test3);
System.out.println("Test 3 result: " + result); // expect 5
// Test 4
String test4 = "oolong; ; herbal; ;; ; matcha";
result = countKeywords(test4);
System.out.println("Test 4 result: " + result); // expect 3
// Test 5
String test5 = ";hoki;;h//;;; ; ; ; ;i;;";
result = countKeywords(test5);
System.out.println("Test 5 result: " + result); // expect 3
}
}