public class Solution { public static void main(String[] args) throws IOException { ArrayList<String> strings = new ArrayList<>(); Scanner scanner = new Scanner(System.in); for (int i = 0; i<10; i++){ strings.add(scanner.nextLine()); } String x = strings.get(0); for (String s : strings){ if (s.length()>=x.length()){ x = s; } else { System.out.println(s); break; } } } } Last requirement won't pass