I've tried this a few different ways and not sure what I'm missing. I'm receiving the longest string(s) each time. One solution I do not have commented in I created a second array list that only contained the longest string(s).
package com.codegym.task.task07.task0708;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
/*
Longest string
*/
public class Solution {
private static ArrayList<String> strings = new ArrayList<>();
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int longestLength = 0;
/**
for(int i = 0; i < 5; i++){
strings.add(br.readLine());
if(strings.get(i).length() > longestLength)
longestLength = strings.get(i).length();
}
for(int i = 0; i < strings.size(); i++){
if(strings.get(i).length() == longestLength)
System.out.println(strings.get(i));
}
**/
for(int i = 0; i < 5; i++){
strings.add(br.readLine());
if(strings.get(i).length() > longestLength)
longestLength = strings.get(i).length();
}
int i = 0;
while(i < strings.size()){
if(strings.get(i).length() != longestLength)
strings.remove(i);
else
i++;
}
for(int j = 0; j < strings.size(); j++){
System.out.println(strings.get(j));
}
}
}