I'm getting output the longest string in the list, still it isn't qualifying the task.
Then what should I do for several strings with equal length to be displayed? Can't think something for that!
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 {
//write your code here
String s = "", longest = "";
int max = 0;
strings = new ArrayList<String>();
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
for(int i = 0; i < 5; i++) {
s = reader.readLine();
strings.add(s);
}
for(int i = 0; i < 5; i++) {
max = strings.get(0).length();
if(strings.get(i).length() > max) {
max = strings.get(i).length();
longest = strings.get(i);
}
/*
else if(strings.get(i).length() == max) {
max1 = strings.get(i).length();
longest1 = strings.get(i);
}
*/
}
System.out.println(longest);
/*
if(longest1 != null)
System.out.println(longest1);
*/
}
}
/* Thus when adding elements no position is required, appended to the end.
But when printing, giving which position needs to be printed is required exactly!
*/