CodeGym
Promotion
CodeGym University
Learning
Course
Tasks
Surveys & Quizzes
Games
Help
Schedule
Community
Users
Forum
Chat
Articles
Success stories
Activity
Reviews
Subscriptions
Light theme
Start learning now
  • All questions
Rich
Level 23
San Diego
  • 31.05.2020
  • 607views
  • 3comments

correct output

Question about the task Duplicating words
Java Syntax,  Level 7,  Lesson 9
Resolved

1. Read 10 words from the keyboard and add them to a list of strings.

2. The doubleValues method should duplicate words like this:
alpha, beta, gamma, -> alpha, alpha, beta, beta, gamma, gamma.

3. Display the result, each value on a new line.

Requirements:
  • Declare a string list variable and immediately initialize it.
  • Read 10 lines from the keyboard and add them to the list.
  • The doubleValues method should duplicate list elements like this: alpha, beta, gamma, -> alpha, alpha, beta, beta, gamma, gamma.
  • Display the resulting list, each element on a new line.
package com.codegym.task.task07.task0717; import java.io.BufferedReader; import java.io.InputStreamReader; import java.util.ArrayList; /* Duplicating words */ public class Solution { public static void main(String[] args) throws Exception { // Read strings from the console and declare an ArrayList here BufferedReader r = new BufferedReader(new InputStreamReader(System.in)); ArrayList<String> list = new ArrayList<String>(); for (int i=0; i<10; i++) list.add(r.readLine()); ArrayList<String> result = doubleValues(list); for (String x : list) System.out.println(x); // Display result } public static ArrayList<String> doubleValues(ArrayList<String> list) { //write your code here //ArrayList<String> st = new ArrayList<String>(); for(int i=0; i<list.size(); i+=2) list.add(i, list.get(i)); return null; } }
+1
Comments (3)
  • Popular
  • New
  • Old
You must be signed in to leave a comment
Adarsh
Level 11 , Bhopal, India
5 June 2020, 02:47
on line 35 you should return list instead of null.
0
Rich
Level 23 , San Diego, United States
31 May 2020, 04:18
ah thank you.
0
Brandon
Level 9 , Toledo, United States
31 May 2020, 04:06
It does seem to work..however I'd wager they want you to use the returned result of the method "doubleValues". Yours returns null, and your "ArrayList<String> result" is never used.
+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.