I have tested my program every way I can think of, and it seems to print out the right answer (the longest string(s)) every time, but when I go to verify, it says I fail the last two requirements. What the heck am I doing wrong?
package com.codegym.task.task07.task0708;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
/*
Longest string
*/
public class Solution {
private static List<String> strings;
public static void main(String[] args) throws Exception {
strings = new ArrayList<String>();
for (int i = 0; i < 5; i++) {
String s = null;
strings.add(s);
}
strings = stringGet(strings);
// ArrayList<String> long = findLong(strings);
printLong(findLong(strings));
}
public static List<String> stringGet(List<String> x) throws IOException {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
for (int i = 0; i < 5; i++) {
String s = reader.readLine();
x.set(i, s);
}
return x;
}
public static ArrayList<String> findLong(List<String> x) {
ArrayList<String> longest = new ArrayList<String>();
longest.add(x.get(1));
for (int i = 0; i < x.size() - 1; i++) {
int l = x.get(i).length();
int m = x.get(i+1).length();
if (m > l) {
int j = longest.size()-1;
while (j > 0) {
longest.remove(1);
j--;
}
longest.set(0, x.get(i + 1));
}
else if (m == l) {
longest.add(x.get(i+1));
}
}
return longest;
}
public static void printLong(ArrayList<String> x) {
for (int i = 0; i < x.size(); i++) {
System.out.println(x.get(i));
}
}
}