Not sure why the program is only giving the shortest string. Any ideas?
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
*/
public class Solution {
public static void main(String[] args) throws Exception {
ArrayList<String> list = new ArrayList<String>();
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
boolean flag = false;
for(int i=0;i<10;i++) {
String s = reader.readLine();
list.add(s);
}
String shortest = list.get(0);
String longest = list.get(0);
for(int i=0;i<list.size();i++) {
if(shortest.length() > list.get(i).length()) {
shortest = list.get(i);
flag = false;
}
if(longest.length() < list.get(i).length()) {
longest = list.get(i);
}
}
if(flag == false) {
System.out.println(shortest);
} else {
System.out.println(longest);
}
}
}