KaiquanMah commited on
Commit
b4c9a50
·
verified ·
1 Parent(s): 577b94b

str.substring equals; str.charAt ==; str.endsWith

Browse files
Week 7: Enum, Generic Type, Streams, write to file, class diagram/11B. Sentences Ending with a Period ADDED
@@ -0,0 +1,89 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ Write a method:
2
+ public static ArrayList<String> sentencesWithPeriod(ArrayList<String> sentences)
3
+
4
+ that takes a list of sentences as strings as its parameter.
5
+ The method returns a new list containing only those sentences from the original list that end with a period.
6
+ Use 'stream' for your solution
7
+
8
+
9
+
10
+ import java.util.Random;
11
+ import java.util.ArrayList;
12
+ import java.util.stream.Collectors;
13
+
14
+ public class Test {
15
+ public static void main(String[] args) {
16
+ final Random r = new Random();
17
+
18
+
19
+ String[] sub = "Pike Perch Roach Rose Poppy Lily Rabbit Bunny Hare Java Python".split(" ");
20
+ String[] ob = "fish flower plant animal mammal programming language".split(" ");
21
+
22
+ ArrayList<String> sentences = new ArrayList<>();
23
+ int m = r.nextInt(6) + 6;
24
+ for (int i = 0; i < m; i++) {
25
+ String sentence = sub[r.nextInt(sub.length)] + " is a " + ob[r.nextInt(ob.length)];
26
+ sentence += r.nextInt(2) == 0 ? "." : "";
27
+ sentences.add(sentence);
28
+ }
29
+
30
+ System.out.println("Sentences:");
31
+ sentences.stream().forEach(s -> System.out.println("" + s));
32
+
33
+ System.out.println("Sentences ending with a period:");
34
+ sentencesWithPeriod(sentences).stream().forEach(s -> System.out.println(s));
35
+ }
36
+
37
+
38
+ //ADD
39
+ public static ArrayList<String> sentencesWithPeriod(ArrayList<String> sentences) {
40
+ // approach 1 - substring
41
+ // substring() returns a new string
42
+ // => which is a reference type object
43
+ // => so compare using .equals()
44
+ //https://stackoverflow.com/questions/5163785/how-do-i-get-the-last-character-of-a-string
45
+ return sentences.stream().
46
+ filter(str -> str.substring(str.length() - 1).equals(".")).
47
+ collect(Collectors.toCollection(ArrayList::new));
48
+
49
+
50
+ // approach 2 - charAt
51
+ // charAt() returns a char
52
+ // => which is a primitive type
53
+ // => so compare using '=='
54
+ return sentences.stream().
55
+ filter(str -> str.charAt(str.length() - 1) == '.').
56
+ collect(Collectors.toCollection(ArrayList::new));
57
+
58
+
59
+ // approach 3 - endsWith
60
+ // endsWith() returns a boolean
61
+ // => naturally 'true' elements are kept
62
+ return sentences.stream().
63
+ filter(str -> str.endsWith(".")).
64
+ collect(Collectors.toCollection(ArrayList::new));
65
+
66
+ }
67
+
68
+
69
+
70
+
71
+
72
+ }
73
+
74
+
75
+
76
+
77
+ Sentences:
78
+ Bunny is a programming.
79
+ Pike is a plant.
80
+ Bunny is a mammal
81
+ Java is a flower
82
+ Hare is a animal
83
+ Lily is a programming.
84
+
85
+ Sentences ending with a period:
86
+ Bunny is a programming.
87
+ Pike is a plant.
88
+ Lily is a programming.
89
+