-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Expand file tree
/
Copy pathArrayListPractice.java
More file actions
47 lines (42 loc) · 1.43 KB
/
ArrayListPractice.java
File metadata and controls
47 lines (42 loc) · 1.43 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
package exercises.Lesson2;
import java.util.Arrays;
import java.util.Scanner;
import java.util.List;
import java.util.ArrayList;
public class ArrayListPractice {
/*Exercise 1*/
public static int evenSum(ArrayList<Integer> arr) {
int sum = 0;
for (int number : arr) {
if (number % 2 == 0) {
sum += number;
}
}
return sum;
}
/*Exercise 2 + 3*/
public static void printLetters(ArrayList<String> arr) {
Scanner input = new Scanner(System.in);
System.out.println("Enter number of letters: ");
int numOfLetters = input.nextInt();
for (String word : arr) {
if (word.length() == numOfLetters) {
System.out.println(word);
}
}
}
public static void main(String[] args) {
ArrayList<Integer> values = new ArrayList<>();
for (int i = 0; i < 10; i++) {
values.add(i);
}
/*BONUS Exercise 3*/
String sentence = "I would not, could not, in a box. I would not, could not with a fox. I will not eat them in a house. I will not eat them with a mouse.";
sentence = sentence.replace(",", "");
sentence = sentence.replace(".", "");
String[] sentenceSplit = sentence.split(" ");
ArrayList<String> wordsArrList = new ArrayList<>(
Arrays.asList(sentenceSplit));
printLetters(wordsArrList);
}
}