In the first for-loop, then initializes and finds the longest string and puts it at index 0.
The second for-loop checks for any other smaller strings in the list and removes it then displays.
The output is right according to the condition but it still doesn't satisfy it.
package com.codegym.task.task07.task0708;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
/*
1. Initialize the list of strings.
2. Read 5 strings from the keyboard and add them to this list.
3. Using a loop, find the longest string in the list.
4. Display the string. If there is more than one, display each on a new line.
*/
public class Solution {
private static List<String> strings;
public static void main(String[] args) throws Exception {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
strings = new ArrayList<String>();
ArrayList<String> longest = new ArrayList<String>(5);
strings.add(reader.readLine());
longest.add(strings.get(0));
for (int i=1; i<5; i++) {
strings.add(reader.readLine());
if (((longest.get(0)).length()) > ((strings.get(i)).length())) {
continue;
}
else if (((longest.get(0)).length()) < ((strings.get(i)).length())) {
longest.set(0,(strings.get(i)));
}
else {
longest.add(strings.get(i));
}
}
for (int i=0; i<(longest.size()); i++) {
if (((longest.get(0)).length()) > ((longest.get(i)).length())) {
longest.remove(i);
continue;
}
System.out.println(longest.get(i));
}
}
}