I have tried many different things but it doesn't seem to be working. The final code may not be the perfect but as I said I tried many things and finally asking for the help.
May be i am not been able to understand what's actually expected. Anyone can help me with verification pass.
package com.codegym.task.task14.task1420;
/*
GCD
*/
import java.io.BufferedReader;
import java.io.InputStreamReader;
public class Solution {
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String s1 = br.readLine();
String s2 = br.readLine();
int GCD;
boolean isNumber1 = parseNumber(s1);
boolean isNumber2 = parseNumber(s2);
if( isNumber1 == true && isNumber2 == true){
int n1 = Integer.parseInt(s1);
int n2 = Integer.parseInt(s2);
GCD = getGCD(n1,n2);
System.out.println(GCD);
}else{
throw new Exception ("entered value is either -ve or a string");
}
}
public static boolean parseNumber(String s1) throws Exception{
boolean isNum = false;
try{
int n1 = Integer.parseInt(s1);
if(n1 < 0 ){
throw new Exception("Please enter +ve number");
}else{
isNum = true;
}
}catch(NumberFormatException e){
throw new Exception("Please enter +ve number");
}finally{
return isNum;
}
}
public static int getGCD(int n1, int n2){
int gcd = 0;
for(int i = 1; i <= n1 && i <= n2; ++i)
{
// Checks if i is factor of both integers
if(n1 % i == 0 && n2 % i == 0)
gcd = i;
}
return gcd;
}
}