I have tested this program and it always displays the longest string(s). I also passed the display of multiple strings if multiple are longest, I am not sure how I can meet req 4 and not req 3. Below is the input (lines 1-5) and output (line 6) of the test another question was told to run:
qwe qwe
test1
test2
test3
test4
qwe qwe
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 List<String> strings = new ArrayList<>();
public static void main(String[] args) throws Exception {
//write your code here
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
for (int i = 0; i<5; i++){
String s = reader.readLine();
strings.add(s);
}
int lword = 0;
for (int i = 0; i<strings.size()-1; i++) {
String s = strings.get(i);
int a = s.length();
if (a >= lword) {
lword = a;
}
}
for (int i = 0; i<strings.size(); i++){
if (strings.get(i).length() == lword) {
System.out.println(strings.get(i));
}
}
}
}