Hi guys,
for some reason I can't pass condition for filling all numbers to list2 (x % 2 == 0).
It seems I miss last number (150) in the list.
Can anyone see why? Thanks.
package com.codegym.task.task07.task0713;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
/*
Playing Javarella
*/
public class Solution {
public static void main(String[] args) throws Exception {
//write your code here
//initialize arrays
ArrayList<Integer> list = new ArrayList<Integer>();
ArrayList<Integer> list1 = new ArrayList<Integer>();
ArrayList<Integer> list2 = new ArrayList<Integer>();
ArrayList<Integer> list3 = new ArrayList<Integer>();
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
//read from keyboard
for (int i = 0; i < 20; i++) {
//add to the list
list.add(Integer.parseInt(in.readLine()));
}
//sorting list into subsequent lists
for (int i = 0; i < list.size(); i++) {
//adding to the first supplementary list
if (list.get(i) % 3 == 0) {
list1.add(list.get(i));
}
else if (list.get(i) % 2 == 0) {
//adding to the second supplementary list
list2.add(list.get(i));
}
else if (list.get(i) % 3 == 0 && list.get(i) % 2 == 0) {
//adding to both first and second supplementary list
list1.add(list.get(i));
list2.add(list.get(i));
}
else if (list.get(i) % 3 != 0 && list.get(i) % 2 != 0) {
//adding to the third supplementary list
list3.add(list.get(i));
}
}
printList(list1);
printList(list2);
printList(list3);
}
public static void printList(List<Integer> list) {
//write your code here
for (int i = 0; i < list.size(); i++) {
System.out.println(list.get(i));
}
}
}