Skip to content

Commit 5db1199

Browse files
authored
Update Activity01.java
modifying the example to coincide with the book and offering an alternative solution
1 parent 874ef21 commit 5db1199

File tree

1 file changed

+19
-10
lines changed

1 file changed

+19
-10
lines changed

Chapter04/Activity01/Activity01.java

+19-10
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,30 @@
11
import java.util.Arrays;
22

3-
public class Activity01 {
3+
public class Activity1 {
44
public static void main(String[] args) {
55
String[] text = {"So", "many", "books", "so", "little", "time"};
6-
String toSearch = "so";
7-
int occurrences = -1;
6+
String searchQuery = "so";
7+
int occurrence = -1;
88

9-
for(int i = 0; i < 5; i++) {
10-
if (text[i].compareToIgnoreCase(toSearch) == 0) {
9+
for(int i = 0; i < text.length; i++) {
10+
occurrence = text[i].compareToIgnoreCase(searchQuery);
11+
if (occurrence == 0) {
1112
System.out.println("Found query at :" + i);
12-
occurrences++;
13-
}
13+
}
1414
}
1515

16-
if (occurrences > -1) {
17-
occurrences++;
18-
System.out.println("Found: " + occurrences + " coinciding words");
16+
/* Alternative solution displaying the total amount of findings at the end
17+
for(int i = 0; i < text.length; i++) {
18+
if (text[i].compareToIgnoreCase(searchQuery) == 0) {
19+
System.out.println("Found query at :" + i);
20+
occurrence++;
21+
}
22+
}
23+
24+
if (occurrence > -1) {
25+
occurrence++;
26+
System.out.println("Found: " + occurrence + " coinciding words");
1927
}
28+
*/
2029
}
2130
}

0 commit comments

Comments
 (0)