If I will leave private static ArrayList as default in the solution (for example on task restart) condition 3 and 4 work. Also program has correct output because I have tested it on several words - always showed correctly. Please help me
package pl.codegym.task.task07.task0708;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
/*
Najdłuższy ciąg
*/
public class Solution {
private static ArrayList<String> strings = new ArrayList<>();;
public static void main(String[] args) throws Exception {
// ArrayList<String> strings = new ArrayList<>();
BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
int min = 0;
for (int i = 0; i<5; i++) {
strings.add(bf.readLine());
}
for (int i = 0; i<strings.size(); i++) {
if (min < strings.get(i).length())
min = strings.get(i).length();
}
for (int i = 0; i<strings.size(); i++) {
if (min == strings.get(i).length())
System.out.println(strings.get(i));
}
}
}
/*1. Inicjalizuj listę ciągów.
2. Wczytaj 5 ciągów z klawiatury i dodaj je do listy.
3. Używając pętli, znajdź najdłuższy ciąg na liście.
4. Wyświetl ten ciąg. Jeśli jest ich więcej, wyświetl każdy z nich w nowej linii.*/