Spaces:
Running
Running
Create 19. Count shorter strings
Browse files
Week 2: Methods, strings and lists/19. Count shorter strings
ADDED
|
@@ -0,0 +1,81 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
Write the method
|
| 2 |
+
|
| 3 |
+
int countShorter(ArrayList<String> strings, int length)
|
| 4 |
+
|
| 5 |
+
which counts the number of strings in the list string that have less than length characters.
|
| 6 |
+
|
| 7 |
+
|
| 8 |
+
Example method call:
|
| 9 |
+
public static void main(String[] args){
|
| 10 |
+
ArrayList<String> test = new ArrayList<>();
|
| 11 |
+
test.add("Huu");
|
| 12 |
+
test.add("Haa");
|
| 13 |
+
test.add("Huhuu");
|
| 14 |
+
test.add("Hohoo");
|
| 15 |
+
|
| 16 |
+
int shorterthan4= countShorter(test, 4);
|
| 17 |
+
System.out.println(shorterthan4);
|
| 18 |
+
}
|
| 19 |
+
|
| 20 |
+
Program outputs:
|
| 21 |
+
2
|
| 22 |
+
|
| 23 |
+
|
| 24 |
+
|
| 25 |
+
|
| 26 |
+
|
| 27 |
+
|
| 28 |
+
import java.util.Random;
|
| 29 |
+
import java.util.ArrayList;
|
| 30 |
+
import java.util.Arrays;
|
| 31 |
+
import java.util.List;
|
| 32 |
+
|
| 33 |
+
public class Test{
|
| 34 |
+
public static void main(String[] args){
|
| 35 |
+
final Random r = new Random();
|
| 36 |
+
|
| 37 |
+
test(new String[] {"first", "second", "third", "fourth", "five", "six", "7"}, 5);
|
| 38 |
+
test("first second third fourth programming class method java variable".split(" "), 6);
|
| 39 |
+
test("a ab abc abcd xyz xy x z".split(" "), 2);
|
| 40 |
+
}
|
| 41 |
+
|
| 42 |
+
public static void test(String[] l, int length) {
|
| 43 |
+
ArrayList<String> list = new ArrayList<>(Arrays.asList(l));
|
| 44 |
+
System.out.println("Testing with list " + list);
|
| 45 |
+
System.out.print("Strings, that contain under " + length + " characters: ");
|
| 46 |
+
System.out.println(countShorter(list, length));
|
| 47 |
+
System.out.println("");
|
| 48 |
+
}
|
| 49 |
+
|
| 50 |
+
|
| 51 |
+
//ADD
|
| 52 |
+
public static int countShorter(ArrayList<String> strings, int length) {
|
| 53 |
+
int count = 0;
|
| 54 |
+
for (String str: strings) {
|
| 55 |
+
if (str.length() < length) {
|
| 56 |
+
count++;
|
| 57 |
+
}
|
| 58 |
+
}
|
| 59 |
+
return count;
|
| 60 |
+
}
|
| 61 |
+
|
| 62 |
+
|
| 63 |
+
|
| 64 |
+
}
|
| 65 |
+
|
| 66 |
+
|
| 67 |
+
|
| 68 |
+
|
| 69 |
+
|
| 70 |
+
|
| 71 |
+
Testing with list [first, second, third, fourth, five, six, 7]
|
| 72 |
+
Strings, that contain under 5 characters: 3
|
| 73 |
+
|
| 74 |
+
Testing with list [first, second, third, fourth, programming, class, method, java, variable]
|
| 75 |
+
Strings, that contain under 6 characters: 4
|
| 76 |
+
|
| 77 |
+
Testing with list [a, ab, abc, abcd, xyz, xy, x, z]
|
| 78 |
+
Strings, that contain under 2 characters: 3
|
| 79 |
+
|
| 80 |
+
|
| 81 |
+
|