I'm having some problems with the verification part, i.e. this condition -> "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."
If there is some problem with the code to fulfil the requirements, please help me with that. :(
p.s. I've tried this code with several other inputs in the list.
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("lyre"); // 1
list.add("love"); // 2
list.add("wade");
list.add("bulk");
list.add("measure");
list = fix(list);
for (int i = 0; i < list.size(); i++) {
System.out.println(list.get(i));
}
}
public static ArrayList<String> fix(ArrayList<String> list) {
int r = 0; int l = 0;
ArrayList<String> str = new ArrayList<String>();
for(int i = 0; i < list.size(); i++) {
for(int j = 0; j < list.get(i).length(); j++) {
char ch = list.get(i).charAt(j);
if(ch == 'r')
r++;
if(ch == 'l')
l++;
}
if(r > 0 && l == 0)
list.remove(i);
else if(r == 0 && l > 0) {
str.add(list.get(i));
}
r = 0;
l = 0;
}
list.addAll(str);
return list;
}
}