I do not understand why
} else if ((t.length() == result.get(0).length()))
and
if ((t.length() < s.length()) && (s.length() != result.get(0).length()))
returns as true when it is not. I have tested my code with:
grandfather
grandmother
daughter
program
car
and
grandmother
daughter
program
car
bar
It will find the shortest string but it will not save multiple strings of the same length. Any assistance would be appreciated
package en.codegym.task.jdk13.task07.task0709;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
/*
Expressing ourselves more concisely
*/
public class Solution {
public static void main(String[] args) throws Exception {
//write your code here
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
ArrayList<String> list = new ArrayList<String>();
for (int i = 0; i < 5; i++) {
String s = reader.readLine();
list.add(s);
}
ArrayList<String> result = new ArrayList<String>();
result.add(list.get(0));
for (int i = 0; i < list.size(); i++) {
String t = list.get(i);
byte counter = 0;
for (String s : list) {
if ((t.length() < s.length()) && (t.length() < result.get(0).length())) {
result.remove(0);
result.add(0, t);
} else if ((t.length() == result.get(0).length())) {
for (int j = 0; j < result.size(); j++) {
if(!t.equals(result.get(j)) && counter == 0){
result.add(1, t);
counter++;
}
}
}
}
}
for (String s : result) {
System.out.println(s);
}
}
}