What's wrong? I've tested both versions several times and work perfectly fine
package com.codegym.task.task07.task0708;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.*;
/*
Longest string
*/
public class Solution {
private static List<String> strings;
public static void main(String[] args) throws Exception {
//write your code here
Scanner scanner = new Scanner(System.in);
strings = new ArrayList<>();
/*
First version
String longest = scanner.next();
strings.add(longest);
for (int i = 1; i < 5; i++) {
String string = scanner.next();
strings.add(string);
if (longest.length() < strings.get(i).length()) {
longest = strings.get(i);
}
}
for (String i : strings){
if (i.length() == longest.length()){
System.out.println(i);
}
}
*/
/*
Second version
*/
for (int i = 0; i < 5; i++) {
strings.add(scanner.next());
}
int longest = strings.get(0).length();
for (int i = 0; i < strings.size(); i++) {
if (longest < strings.get(i).length()){
longest = strings.get(i).length();
}
}
for (int i = 0; i < strings.size(); i++) {
if (strings.get(i).length() == longest){
System.out.println(strings.get(i));
}
}
}
}