My code does what the task asks for but it doesn't meet the requirements
package com.codegym.task.task07.task0708;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.*;
import java.util.List;
/*
Longest string
*/
public class Solution {
private static List<String> strings;
public static void main(String[] args) throws Exception {
Scanner sc = new Scanner(System.in);
strings = new ArrayList<String>();
ArrayList<Integer> sizes = new ArrayList<Integer>();
for (int i = 0; i < 5; i++) {
strings.add(sc.nextLine());
sizes.add(strings.get(i).length());
}
for (int i = 4; i > 0; i--) {
int j = i - 1;
if (sizes.get(i) < sizes.get(j)) {
strings.remove(i);
sizes.remove(i);
continue;
}
if (sizes.get(i) > sizes.get(j)) {
strings.remove(j);
sizes.remove(j);
}
}
strings.forEach(System.out::println);
}
}