for (int i=0; i<list.size(); i++){
shortest = list.get(0).length();
if (list.get(i).length()<shortest)
shortest=list.get(i).length();
// longest = list.get((0)).length();
if (list.get(i).length()>longest)
longest=list.get(i).length();
}
package com.codegym.task.task07.task0712;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
/*
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.
*/
public class Solution {
public static void main(String[] args) throws Exception {
BufferedReader bfr = new BufferedReader(new InputStreamReader(System.in));
ArrayList<String> list = new ArrayList<>();
for (int i=0; i<10; i++){
list.add(bfr.readLine());
}
int shortest=0;
int longest=0;
for (int i=0; i<list.size(); i++){
shortest = list.get(0).length();
if (list.get(i).length()<shortest)
shortest=list.get(i).length();
// longest = list.get((0)).length();
if (list.get(i).length()>longest)
longest=list.get(i).length();
}
for (int i=0; i<list.size(); i++){
if ( list.get(i).length()==shortest || list.get(i).length()==longest ){
System.out.println(list.get(i));
break;
}
}
}
}