Why I got this error/ im making a list with 20 numbers? What is wrong?
package com.codegym.task.task07.task0713;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
/*
Playing Javarella
1. Enter 20 numbers from the keyboard, save them in a list, and then sort them to three other lists:
Numbers divisible by 3 (x%3==0), numbers divisible by 2 (x%2==0), and all other numbers.
Numbers simultaneously divisible by 3 and 2 (for example 6) go into both lists.
The order in which the lists are declared is very important.
2. The printList method should display each list item on a new line.
3. Using the printList method, display these three lists. First, the list for x%3, then the list for x%2, and then the last list.
*/
public class Solution {
public static void main(String[] args) throws Exception {
//write your code here
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
ArrayList<Integer> list = new ArrayList<>();
ArrayList<Integer> list1 = new ArrayList<>();
ArrayList<Integer> list2 = new ArrayList<>();
ArrayList<Integer> list3 = new ArrayList<>();
for (int i = 0; i < 20; i++) {
String re = bufferedReader.readLine();
int number = Integer.parseInt(re);
list.add(number);
}
for (int i = 0; i < list.size(); i++) {
int num = list.get(i);
if (num % 3 == 0) {
list1.add(num);
if (num % 2 == 0) {
list2.add(num);
}
} else if (num % 2 == 0) {
list2.add(num);
} else {
list3.add(num);
}
}
list.addAll(list1);
list.addAll(list2);
list.addAll(list3);
printList(list);
printList(list1);
printList(list2);
printList(list3);
}
public static void printList(List<Integer> list) {
//write your code here
for (Integer integer : list) {
System.out.println(integer);
}
}
}