Here are a few minor things I found:
1. There was something wrong with the algorithm used for finding gcd.
2. The class DivisorCalc has all its variables declared static which by default get initialized to 0 and thats why the whilie block in Gcd() is never run.
while (dividend1 != dividend2)
{ /*dividend1, and dividend2 are always 0*/
Heres something to remember- Make a variable static when you *really* want just one one copy of the variable inspite of the number of instances of class. Same goes for a static method/block.
3. In class GcdDriver the call to gcd() should have been in the while loop.
/**
* GcdDriver
*/
import DivisorCalc;
import java.util.StringTokenizer;
import java.io.*;
public class GcdDriver
{
public static void main(String args[]) throws IOException
{
StringTokenizer tokenizer;
String line, file = "numbers.txt";
int num1 = 0, num2 = 0, answer = 0;
try
{
FileReader fr = new FileReader (file);
BufferedReader inFile = new BufferedReader (fr);
line = inFile.readLine();
while (line != null)
{
tokenizer = new StringTokenizer (line);
try
{
num1 = Integer.parseInt(tokenizer.nextToken());
num2 = Integer.parseInt(tokenizer.nextToken());
answer = DivisorCalc.gcd(num1, num2);
System.out.println ("Greatest common divisor:" + answer);
}
catch (NumberFormatException exception)
{
System.out.print("Error in input. Line ignored:");
System.out.println (line);
}
line = inFile.readLine();
}
inFile.close();
}
catch (FileNotFoundException exception)
{
System.out.println ("The file " + file + "was not found.");
}
catch (IOException exception)
{
System.out.println (exception);
}
}
}
/**
* DivisorCalc
*/
public class DivisorCalc
{
/**
* What is happening here??
*
private static int dividend1, dividend2, num1, num2;
private static int max, answer, result;
public static int Gcd (int num1, int num2)
{
if(num1 > num2) // to figure larger of the two numbers
max = num1;
else
max = num2;
while (dividend1 != dividend2)
{
for ( int i = 1; i < max; i++)
dividend1 = num1 - max;
dividend2 = num2 - max;
max--;
if (dividend1 == dividend2)
answer = dividend1;
}
return answer;
} //class Gcd
*/
/**
* Completing problem 1 presented at
* <url>http://www.math.ucla.edu/~ronmiech/Pic20/Sexampdf/Sex.pdf</url>
* Recursion is another way.
*/
public static int gcd(int a, int b)
{
if(a == 0 || b == 0) return 0;
int A = Math.abs(a);
int B = Math.abs(b);
int c = Math.max(A,B);
int d = Math.min(A,B);
int r = c%d;
while( r != 0)
{
c = d;
d = r;
r = c % d;
}
return d;
}
} // class DivisorCalc
numbers.txt
45 15
I Love Lucy
100 35
4 7