I have tested the code with different inputs and it gives the right output but not passing the most tests in codegym. I also tried changing List<String> to ArrayList<String> in the instant variable still no luck. Anybody please explain what I am doing wrong here?
Thanks.
package com.codegym.task.task07.task0709;
import java.util.*;
import java.io.*;
class Solution {
private static List<String> list = new ArrayList<String>();
public static void main(String[] args) throws Exception {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
ArrayList<String> shortWords = new ArrayList<String>();
String shortest = "";
for(int i = 0; i < 5; i++) {
list.add(reader.readLine());
}
Collections.sort(list);
shortest = list.get(0);
for(int j = 1; j < list.size(); j++) {
if(shortest.length() == list.get(j).length()) {
shortWords.add(list.get(j));
}
}
shortWords.add(shortest);
for(String temp : shortWords) {
System.out.println(temp);
}
}
}