I don't know what I am doing wrong, it seems like if the numbers end up being negative that the program will throw the exception. But I am still failing the tests.
package com.codegym.task.task14.task1420;
/*
GCD
*/
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.text.NumberFormat;
public class Solution {
public static void main(String[] args) throws Exception {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
int n1 = 0;
int n2 = 0;
String s = reader.readLine();
n1 = Integer.parseInt(s);
String k = reader.readLine();
n2 = Integer.parseInt(k);
if (n1 < 0 || n2 < 0) {
throw new NumberFormatException();
}
int gcd = gcdByBruteForce(n1, n2);
System.out.println(gcd);
}
public static int gcdByBruteForce(int n1, int n2) {
int gcd = 1;
for (int i = 1; i <= n1 && i <= n2; i++) {
if (n1 % i == 0 && n2 % i == 0) {
gcd = i;
}
}
return gcd;
}
}