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
Nishant Bhatia
Level 13
Navi Mumbai
  • 12.06.2019
  • 1348views
  • 3comments

Can anyone please identify the problem?

Question about the task To the top of the list
Java Syntax,  Level 7,  Lesson 6
Resolved


1. Create a list of strings in the main method.
2. Add 10 strings to it from the keyboard, but only add them to the beginning of the list, not the end.
3. Use a loop to display the contents, each value on a new line.

Requirements:
  • Declare a string list variable and immediately initialize it.
  • The program should read 10 strings from the keyboard and add them to the list.
  • The program should add lines to the beginning of the list.
  • The program should display the list, each value on a new line.
package com.codegym.task.task07.task0710; import java.io.BufferedReader; import java.io.InputStream; import java.io.InputStreamReader; import java.util.ArrayList; /* To the top of the list */ public class Solution { public static void main(String[] args) throws Exception { //write your code here ArrayList<String> strings = new ArrayList<>(); InputStream in = System.in; BufferedReader reader = new BufferedReader(new InputStreamReader(in)); for(int i = 0; i < 10 ;i++) { strings.add(0,reader.readLine()); } //for(int i = strings.size()-1;i>=0;i--) System.out.println(strings.get(0)+"\n"); } }
0
Comments (3)
  • Popular
  • New
  • Old
You must be signed in to leave a comment
Rich
Level 23 , San Diego, United States
22 May 2020, 02:15
you already have them in the right order. just iterate through using a for each statement (or a regular for) and println for each item.
0
Abraham
Level 22 , Watsonville, United States
12 June 2019, 06:27
When you print the list no need to go in reverse. The print statement has "strings.get(0)", you need "strings.get(i)" You do not need a new line when using "println()" it provides it for you.
+2
Nishant Bhatia
Level 13 , Navi Mumbai, India
12 June 2019, 06:36
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.