CodeGym
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
Juan Gallardo
Level 10
Edmonton
  • 12.11.2018
  • 2072views
  • 8comments

Not sure what the problem is

Question about the task Shuffled just in time
Java Syntax,  Level 7,  Lesson 12
Resolved

Use the keyboard to enter 2 numbers N and M.
Enter N strings and put them in a list.
Move the first M strings to the end of the list.
Display the list, each value on a new line.

Note:
you must not create more than one list.

Requirements:
  • Declare a string list variable and immediately initialize it.
  • Read numbers N and M from the keyboard. Read N strings and add them to the list.
  • Move the first M strings to the end of the list.
  • Display the list, each value on a new line.
package com.codegym.task.task07.task0720; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.ArrayList; /* Shuffled just in time Use the keyboard to enter 2 numbers N and M. Enter N strings and put them in a list. Move the first M strings to the end of the list. Display the list, each value on a new line. */ public class Solution { public static void main(String[] args) throws IOException { BufferedReader a = new BufferedReader(new InputStreamReader(System.in)); ArrayList<String> results = new ArrayList<>(); int N = Integer.parseInt(a.readLine()); int M = Integer.parseInt(a.readLine()); //Populate the list for (int i = 0; i < N; i++) { results.add(a.readLine()); } //Copy the first M numbers to the end of the list for (int i = 0; i < M; i++) { results.add(results.get(i)); } //Remove the first M numbers that are now redundant for (int i = 0; i < M; i++) { results.remove(i); i++; } //Display the list for (String s : results) { System.out.println(s); } } }
0
Comments (8)
  • Popular
  • New
  • Old
You must be signed in to leave a comment
vikr@nt
Level 9 , Cincinatti , United States
14 February 2019, 01:46
haha You dont have to run loop twice. It can be done via this way for (int i=0; i<M; i++){ list.add(list.get(0)); list.remove(0); }
+4
hacks patel
Level 20 , Ahmedabad, India
22 December 2018, 06:44
if (N > M) { String[] r = new String[N]; for (int i = 0; i < N; i++) { list.add(reader.readLine()); } for (int i = 0; i < M; i++) { String s = list.get(i); r[i] = s; } for (int i = 0; i < M; i++) { list.add(r[i]); } for(int i = 0 ; i < M ; i++) { list.remove(0); } }
0
Michael Martin
Level 19 , Arlington, United States
13 November 2018, 02:15solution
What output do you get when you run without verifying?
+2
Juan Gallardo
Level 10 , Edmonton, Canada
14 November 2018, 02:47
//input
5
3
1
2
3
4
5
//output
2
3
5
1
2
3
Huh. I thought it was right. Fixed the removal part like so:
//Remove the first M numbers that are now redundant
 for (int i = 0; i < M; i++) {
            results.remove(0);
        }
+2
Michael Martin
Level 19 , Arlington, United States
14 November 2018, 14:08
Is the second requirement now passing? (reading the numbers and adding to the list)
0
Michael Martin
Level 19 , Arlington, United States
14 November 2018, 14:09
The other problem you are having is you cannot add or remove from a list through a loop like that. You have to use an Iterator. But work on the input requirement first.
0
Juan Gallardo
Level 10 , Edmonton, Canada
15 November 2018, 12:34
Everything is passing now. I didn't have any problems removing through a for() loop
0
Michael Martin
Level 19 , Arlington, United States
15 November 2018, 19:04
Great!
0
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.