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
Vishwambhar Awagan
Level 8
Pune
  • 19.10.2018
  • 1465views
  • 6comments

Running but still giving error

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


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 */ public class Solution { public static void main(String[] args) throws IOException { BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); ArrayList<String> list = new ArrayList<String>(); ArrayList<String> arrsub = new ArrayList<String>(); int N = Integer.parseInt(reader.readLine()); int M = Integer.parseInt(reader.readLine()); for(int i=0;i<N;i++){ list.add(reader.readLine()); } for(int i=0;i<M;i++){ arrsub.add(list.get(i)); list.remove(0); list.add(arrsub.get(i)); } for(int i=0;i<N;i++) System.out.println(list.get(i)); //write your code here } }
0
Comments (6)
  • Popular
  • New
  • Old
You must be signed in to leave a comment
Sergei Polakov
Level 11 , Donets'k, Ukraine
23 April 2019, 10:31
мдя , мучение ... еле понял вводим число N , например 5 вводим число M, например 4 потом вводим N - нное количество текстовых строк , в данном случае 5 стрингов Вот пример 5 4 DFGH1 UYTRT2 FFGJH3 YUTRT4 POI 5 -- вывод вот POI 5 DFGH1 UYTRT2 FFGJH3 YUTRT4 Дальше сами разберетесь, реализовать его несложно
0
Serg
Level 17 , Minsk, Belarus
13 May 2019, 10:15
поддерживаю) понять, что от тебя хотят не просто))
0
Khurram
Level 16 , Lahore, Pakistan
20 October 2018, 05:45
it says in the Note not to use more than one list
+1
Vishwambhar Awagan
Level 8 , Pune, India
20 October 2018, 07:48
Stiil not working bro.. giving same errors. public class Solution { public static void main(String[] args) throws IOException { BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); ArrayList<String> list = new ArrayList<String>(); int N = Integer.parseInt(reader.readLine()); int M = Integer.parseInt(reader.readLine()); for(int i=0;i<N;i++){ list.add(reader.readLine()); } for(int i=0;i<M;i++){ list.add(list.get(i)); list.remove(0); } for(int i=0;i<N;i++) System.out.println(list.get(i)); } }
0
Khurram
Level 16 , Lahore, Pakistan
20 October 2018, 09:06useful
in the loop below; for(int i=0;i<M;i++){ list.add(list.get(i)); list.remove(0); } when you remove the 0 element (position 1), all the other elements move up one place, and i keeps increasing by 1, so the second time the loop runs, it adds the 1 element (position 2) to the end of the list not the 0 element.
+1
Vishwambhar Awagan
Level 8 , Pune, India
21 October 2018, 09:20
thanks..
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.