CodeGym
Promotion
CodeGym University
Learning
Course
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
fifi deng
Level 22
Paris
  • 03.07.2020
  • 251views
  • 2comments

cant understand, why is it?

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; import java.io.IOException; import java.lang.Integer; /* Playing Javarella */ public class Solution { public static void main(String[] args) throws Exception { ArrayList<Integer> array=new ArrayList<Integer>(); ArrayList<Integer> array1=new ArrayList<Integer>(); ArrayList<Integer> array2=new ArrayList<Integer>(); ArrayList<Integer> array3=new ArrayList<Integer>(); BufferedReader reader=new BufferedReader(new InputStreamReader(System.in)); //String line = ""; String line = ""; while((line=reader.readLine())!=null){ array.add(Integer.parseInt(line)); } for(int i=0;i<array.size();i++) { if(array.get(i)%3==0 && array.get(i)%2!=0) array1.add(array.get(i)); if(array.get(i)%2==0 && array.get(i)%3!=0) array2.add(array.get(i)); if(array.get(i)%2==0 && array.get(i)%3==0) {array2.add(array.get(i)); array1.add(array.get(i));} else if(array.get(i)%3!=0 && array.get(i)%2!=0) array3.add(array.get(i)); } //printList(array); printList(array1); printList(array2); printList(array3); //write your code here } public static void printList(List<Integer> list) { for(int i=0;i<list.size();i++) { System.out.println(list.get(i)+" "); // System.out.println(""); } //System.out.println(""); // for(int i=0;i<array2.size();i++) // { // System.out.println(array2.get(i)); // } // for(int i=0;i<array3.size();i++) // { // System.out.println(array3.get(i)); // } //write your code here } }
0
Comments (2)
  • Popular
  • New
  • Old
You must be signed in to leave a comment
Nikita Izvekov
Level 9 , Poughkeepsie, United States
3 July 2020, 03:55
do it with FOR loop. just follow what they want.
0
fifi deng
Level 22 , Paris, France
3 July 2020, 05:07
it meas that just set for loop number is 20, we just read 20 numbers, ok , iget it. thanks
+1
Learn
  • Registration
  • Java Course
  • Help with Tasks
  • Pricing
  • Game Projects
  • 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 Java developer’s career.
Follow us
Interface language
Programmers Are Made, Not Born © 2023 CodeGym
MastercardVisa
Programmers Are Made, Not Born © 2023 CodeGym
This website uses cookies to provide you with personalized service. By using this website, you agree to our use of cookies. If you require more details, please read our Terms and Policy.