I've tried many things to pass this condition.
I've added try/catch blocks.
I've used an if block to throw an Illegal Argument Exception for negative numbers.
I'm currently using an if block to convert negative int into positive int.
I've changed the main signature to what another question post suggested.
package com.codegym.task.task14.task1420;
/*
GCD
Greatest common divisor (GCD).
Enter 2 positive integers from the keyboard.
Display the greatest common divisor.
Requirements:
1. The program should read 2 lines from the keyboard.
2. If the entered lines can't be converted to positive integers, throw an exception.
3. The program should display data on the screen.
4. The program should display the greatest common divisor (GCD) of the numbers read from the keyboard and then terminate successfully.
*/
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Solution {
public static void main(String[] args) throws IOException {
BufferedReader rl = new BufferedReader(new InputStreamReader(System.in));
int a = Integer.parseInt(rl.readLine());
int b = Integer.parseInt(rl.readLine());
if (a < 0){
a = a * -1;
}
if (b < 0){
b = b * -1;
}
System.out.println(gcd(a,b));
}
public static int gcd(int a, int b) {
if (a == 0){
return b;
}
else if ( b == 0){
return a;
}
else {
return (gcd(b, a%b));
}
}
}