package com.codegym.task.task07.task0708;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
/*
Longest string
*/
public class Solution {
private static List<String> strings;
public static void main(String[] args) throws Exception {
strings = new ArrayList<>();
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
int longestString = 0;
for (int i = 0; i < 5; i++) {
strings.add(reader.readLine());
}
for (int i = 0; i < strings.size(); i++) {
if (strings.get(i).length() > longestString)
longestString = strings.get(i).length();
}
for (int i = 0; i < strings.size(); i++) {
if (strings.get(i).length() == longestString)
System.out.println(strings.get(i));
}
}
}
It is already working. I used the help option to check the right answer. But I still didn't understand why we needed to use for loop twice and how we compared the String according to its number of letters??? Can anyone explain please with the most easiest language as you can. I feel like dumb.
Archived
Comments (2)
- Popular
- New
- Old
You must be signed in to leave a comment
Anthony Chalk
13 March 2020, 13:24useful
The first loop is for finding the longest String
The second loop is for finding all Stings that are the same length as the longest String (and displaying them)
+1
Ted404
14 March 2020, 06:37
thank you Anthony. I think I got it now.
0