I don;t see a mistake. In Intelij there also work everything
package pl.codegym.task.task14.task1420;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
public class Solution {
public static void main(String[] args) throws Exception {
BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
try{
int n1 = Integer.parseInt(bf.readLine());
int n2 = Integer.parseInt(bf.readLine());
System.out.println(NWD(n1,n2));
}catch (Exception e){
e.getMessage();
}
}
public static int NWD(int n1,int n2) throws Exception {
int maks = 0;
if (n1<0 || n2<0){
throw new Exception();
}
else{
ArrayList<Integer> dzielnikiN = new ArrayList<>();
ArrayList<Integer> dzielnikiN2 = new ArrayList<>();
ArrayList<Integer> wspolneDzielnikiN1iN2 = new ArrayList<>();
ArrayList<Integer> wspolnyDzielnik = new ArrayList<>();
for(int i=1;i<=n1;i++){
if(n1%i==0){
wspolneDzielnikiN1iN2.add(i);
}
}
for(int i=1;i<=n2;i++){
if(n2%i==0){
wspolneDzielnikiN1iN2.add(i);
}
}
for (int i=0;i<wspolneDzielnikiN1iN2.size();i++){
if (n1%wspolneDzielnikiN1iN2.get(i)==0 && n2%wspolneDzielnikiN1iN2.get(i)==0)
wspolnyDzielnik.add(wspolneDzielnikiN1iN2.get(i));
}
int max = wspolnyDzielnik.get(0);
for (int i=0;i<wspolnyDzielnik.size();i++){
if (wspolnyDzielnik.get(i)>max){
max = wspolnyDzielnik.get(i);
}
}
maks = max;
}
return maks;
}
}