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
Randall John
Level 16
East Cleveland
  • 11.05.2020
  • 472views
  • 4comments

Can't get my code to work, what am I doing wrong?

Question about the task R or L
Java Syntax,  Level 7,  Lesson 9
Under discussion


1. Create a list of words and populate it yourself.
2. The fix method should:
2.1. remove all words containing the letter "r" from the list
2.2. duplicate all words containing the letter "l".
2.3. if a word contains both "r" and "l", then leave it unchanged.
2.4. don't do anything to other words.

For example:
rose
lyre
love

Output:
lyre
love
love

Requirements:
  • The program must not read data from the keyboard.
  • The fix method should remove all words containing the letter "r" from the list. There is an exception: words containing both "r" and "l" should be left alone.
  • The fix method must duplicate words containing the letter "l" (add another instance of this word to the list). There is an exception: words containing both "l" and "r" should not be duplicated.
  • The fix method should not do anything with words that contain both "l" and "r".
  • The fix method should not do anything with words that don't contain either "l" or "r".
package com.codegym.task.task07.task0716; import java.util.ArrayList; /* R or L */ public class Solution { public static void main(String[] args) throws Exception { ArrayList<String> list = new ArrayList<String>(); list.add("rose"); // 0 list.add("love"); // 1 list.add("lyre"); // 2 list = fix(list); for (String s : list) { System.out.println(s); } } public static ArrayList<String> fix(ArrayList<String> list) { for (int i = 0; i < list.size(); i++) { boolean hasR = false; boolean hasL = false; for (int j = 0; j < list.get(i).length(); j++) { if (list.get(i).charAt(j) == 'r') { hasR = true; } if (list.get(i).charAt(j) == 'l') { hasL = true; } } if (hasR == true && hasL == false) { list.remove(i); } else if(hasR == false && hasL == true) { list.add(list.get(i)); } else { //do nothing } } return list; } }
0
Comments (4)
  • Popular
  • New
  • Old
You must be signed in to leave a comment
Randall John
Level 16 , East Cleveland, United States
11 May 2020, 19:48
Thanks everyone! got it
0
Patryk
Level 15 , Katowice, Poland
11 May 2020, 09:02useful
public static List<String> fix(List<String> list) { List<String> list2 = new ArrayList<>(); for(int i = 0; i < list.size(); i++) { if(list.get(i).contains("r")) list2.add( list.get(i).replaceAll("r", "") ); else if (list.get(i).contains("l")) { list2.add(list.get(i)); } else if( list.contains("r") && list.contains("l") ) { //do nothing } else { //do nothing } } System.out.println(list2); return list2; }
+1
Ashish RajAnand
Level 13 , Bhilai , India
11 May 2020, 06:30useful
1.if you copy in same list your answer will wrong. create a separate list 2-And do as said in task but from condition 2.3. to 2.1 . and every thing is good , think and reduce your code as said by baljinder singh.
+1
Baljinder Singh
Level 20 , Toronto, Canada
11 May 2020, 03:34useful
You should not remove or add items into the list while iterating over it I think the better idea would be to create another list and add required items to it Also rather than running a for loop again on each string, it'd be great to use 'contains' method to check if the given string has the substring we are looking for Like this
if(list.get(i).contains("r"))
+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.