My algorithm is as follows:
Fill an ArrayList with all possible divisors of num1, ordered from least to greatest. Starting with the greatest, divide into num2 until the first number is found that successfully divides without a remainder, and that is the GCD for the two numbers. It has worked in every case I have tried, and yet the one condition that won't verify is "The program should display the greatest common divisor (GCD) of the numbers read from the keyboard and then terminate successfully." Can someone please see if you can find an error in my logic?
Thank you, code is below.
J.
package com.codegym.task.task14.task1420;
import java.util.ArrayList;
import java.io.BufferedReader;
import java.io.InputStreamReader;
/*
GCD
*/
public class Solution {
public static boolean isPositive(String i) {
boolean response = true;
int num = 0;
try {
num = Integer.parseInt(i);
} catch (NumberFormatException e) {
response = false;
return response;
}
if (num <= 0) {
response = false;
return response;
}
return response;
}
public static void main(String[] args) throws Exception {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
Exception notPositive = new Exception("Not a positive integer!");
int num1, num2, gsd = 0, tmpNum;
boolean foundgsd = false;
System.out.println("Please enter the first number: ");
String input1 = reader.readLine();
System.out.println("Please enter the second number");
String input2 = reader.readLine();
if (!isPositive(input1))
throw notPositive;
else
num1 = Integer.parseInt(input1);
if (!isPositive(input2))
throw notPositive;
else
num2 = Integer.parseInt(input2);
//Create ArrayLists
ArrayList<Integer> arrayList1 = new ArrayList<>();
ArrayList<Integer> arrayList2 = new ArrayList<>();
//populate list 1 with all divisors of num1
for (int i = 1; i <= (num1); i++) {
if (num1 % i == 0)
arrayList1.add(i);
}
for (int i = arrayList1.size() - 1; i >= 1; i--) {
if (num2 % arrayList1.get(i) == 0) {
gsd = arrayList1.get(i);
foundgsd = true;
break;
}
}
System.out.println(gsd);
}
}

package com.codegym.task.task14.task1420;
import java.util.ArrayList;
import java.io.BufferedReader;
import java.io.InputStreamReader;
/*
GCD
*/
public class Solution {
public static boolean isPositive(String i) {
boolean response = true;
int num = 0;
try {
num = Integer.parseInt(i);
} catch (NumberFormatException e) {
response = false;
return response;
}
if (num <= 0) {
response = false;
return response;
}
return response;
}
public static void main(String[] args) throws Exception {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
Exception notPositive = new Exception("Not a positive integer!");
int num1, num2, gsd = 0, tmpNum;
boolean foundgsd = false;
System.out.println("Please enter the first number: ");
String input1 = reader.readLine();
System.out.println("Please enter the second number");
String input2 = reader.readLine();
if (!isPositive(input1))
throw notPositive;
else
num1 = Integer.parseInt(input1);
if (!isPositive(input2))
throw notPositive;
else
num2 = Integer.parseInt(input2);
//Create ArrayLists
ArrayList<Integer> arrayList1 = new ArrayList<>();
ArrayList<Integer> arrayList2 = new ArrayList<>();
//populate list 1 with all divisors of num1
for (int i = 1; i <= (num1); i++) {
if (num1 % i == 0)
arrayList1.add(i);
}
for (int i = arrayList1.size() - 1; i >= 1; i--) {
if (num2 % arrayList1.get(i) == 0) {
gsd = arrayList1.get(i);
foundgsd = true;
break;
}
}
System.out.println(gsd);
}
}