Longest string
package com.codegym.task.task07.task0708;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
/*
Longest string
1. Initialize the list of strings.
2. Read 5 strings from the keyboard and add them to this list.
3. Using a loop, find the longest string in the list.
4. Display the string. If there is more than one, display each on a new line.
*/
public class Solution {
private static List<String> strings;
public static void main(String[] args) throws Exception {
strings = new ArrayList<>();
BufferedReader bfr = new BufferedReader(new InputStreamReader(System.in));
for(int i=0; i<5; i++){
strings.add(bfr.readLine());
}
int longest=0;
for(int i=0; i<5; i++){
longest = strings.get(0).length();
if (strings.get(i).length()>longest){
longest = strings.get(i).length();
}
}
for (int i=0; i<5; i++) {
if (strings.get(i).length()==longest)
System.out.println(strings.get(i));
}
}
}