The validator expects me to fill my arrayList. I did.
It works fine.
Why?
Could you look at this if you will, please?
package com.codegym.task.task07.task0712;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
/*
Shortest or longest
1. Create a list of strings.
2. Add 10 strings from the keyboard.
3. Find out which string occurs earlier in the list: the shortest or the longest.
If several strings are shortest or longest, then consider the very first such string encountered.
4. Display the string described in Step 3. One string should be displayed.
Requirements:
1. Declare a string list variable and immediately initialize it.
2. The program should read 10 strings from the keyboard and add them to the list.
3. The program should display the shortest string if it comes before the longest.
4. The program should display the longest string if it comes before the shortest.
*/
public class Solution {
public static void main(String[] args) throws Exception {
List<String> arrList = new ArrayList<>();
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String shortest = "";
String longest = "";
for (int i = 0; i < 10; i++) {
arrList.add(br.readLine());
}
for (int i = 0; i < arrList.size(); i++) {
if (i == 0) {
shortest = arrList.get(i);
longest = arrList.get(i);
continue;
}
if (arrList.get(i).length() >= longest.length()) {
longest = arrList.get(i);
arrList.set(0, arrList.get(i));
} else if (arrList.get(i).length() <= shortest.length()) {
shortest = arrList.get(i);
arrList.set(0, arrList.get(i));
}
}
System.out.println(arrList.get(0));
}
}