CodeGym
Promotion
CodeGym University
Learning
Courses
Tasks
Surveys & Quizzes
Games
Help
Schedule
Community
Users
Forum
Chat
Articles
Success stories
Activity
Reviews
Subscriptions
Light theme
Question
  • Reviews
  • About us
Start
Start learning
Start learning now
  • All questions
CodeGym/Help with Java Tasks/Can't find issue
Saheb Das
Level 8
Gurgaon
  • 02.11.2019
  • 928views
  • 2comments

Can't find issue

Question about the task Playing Javarella
Java Syntax,  Level 7,  Lesson 9
Under discussion


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.

Requirements:
  • Declare and immediately initialize 4 ArrayList<Integer> variables. The first list will be the main one. The other lists will be supplementary.
  • Read 20 numbers from the keyboard and add them to the main list.
  • Add to the first supplementary list all numbers in the main list that are divisible by 3.
  • Add to the second supplementary list all numbers in the main list that are divisible by 2.
  • Add to the third supplementary list all the remaining numbers from the main list.
  • The printList method should display each element of the passed list on a new line.
  • The program should display the three supplementary lists using the printList method.
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 BufferedReader reader = new BufferedReader(new InputStreamReader(System.in) ); ArrayList<Integer> list = new ArrayList<Integer>(); for (int i = 0; i<20; i++) { list.add(Integer.parseInt(reader.readLine())); } ArrayList<Integer> list1 = new ArrayList<Integer>(); ArrayList<Integer> list3 = new ArrayList<Integer>(); ArrayList<Integer> list2 = new ArrayList<Integer>(); for (int i = 0; i<20; i++) { Integer x = list.get(i); if (x % 3 == 0){ list1.add(x); // Check that the remainder is zero when we divide by two } //add to the list which are divided by 3 if (x % 2 == 0){ list2.add(x); //add to the list which are divided by 2 } if(x%2!=0 && x%3!=0){ list3.add(x); } } printList(list1); printList(list2); printList(list3); } public static void printList(List<Integer> list) { //write your code here for (Integer temp: list) { System.out.println(temp); } } }
0
Comments (2)
  • Popular
  • New
  • Old
You must be signed in to leave a comment
Abdul-Hamed Mohammed
Level 8 , canton, USA
14 March 2020, 01:04
It is the order in which ArrayList is initialized. It should be in order:
ArrayList<Integer> list = new ArrayList<Integer>();
ArrayList<Integer> list1 = new ArrayList<Integer>();
ArrayList<Integer> list2 = new ArrayList<Integer>();
ArrayList<Integer> list3 = new ArrayList<Integer>();
0
Dawid
Level 28
2 November 2019, 22:11
I honestly don't know what's going on either. So more practically, I can tell you that I did practically the same only separate loops for each condition and instead of writing like list2.add(x) I was writing like list2.add(list.get(i)) but I don't see it changing anything.
0
Learn
  • Registration
  • Java Course
  • Help with Tasks
  • Pricing
  • Java Syntax
Community
  • Users
  • Articles
  • Forum
  • Chat
  • Success Stories
  • Activity
  • Affiliate Program
Company
  • About us
  • Contacts
  • Reviews
  • Press Room
  • CodeGym for EDU
  • FAQ
  • Support
CodeGym CodeGym is an online course for learning Java programming from scratch. This course is a perfect way to master Java for beginners. It contains 1200+ tasks with instant verification and an essential scope of Java fundamentals theory. To help you succeed in education, we’ve implemented a set of motivational features: quizzes, coding projects, content about efficient learning, and a Java developer’s career.
Follow us
Interface language
English
Deutsch Español हिन्दी Français Português Polski বাংলা 简体中文 मराठी தமிழ் Italiano Bahasa Indonesia 繁體中文 Nederlands 日本語 한국어 Bulgarian Danish Hungarian Basa Jawa Malay Norwegian Romanian Swedish Telugu Thai Українська Filipino Turkish Azərbaycan Русский Vietnamese
Programmers Are Made, Not Born © 2025 CodeGym
MastercardVisa
Programmers Are Made, Not Born © 2025 CodeGym