Possible Duplicate:
Division in Java always results in zero (0)?
So I'm writing this program and I thought it was good. The GUI window popped up and I entered a numerator and a demoninator. But no matter what I enter, it always says that it equals 0. So if I put in 2 for the numerator and 3 for the demoninator, the output will be 2/3 = 0. What's the problem?
I changed "int dec" to "double dec" as shown below and put "this.dec = dec" under the Rational class, but that didn't fix anything
import javax.swing.JOptionPane;
public class lab8
{
public static void main (String args[])
{
String strNbr1 = JOptionPane.showInputDialog("Enter Numerator ");
String strNbr2 = JOptionPane.showInputDialog("Enter Denominator ");
int num = Integer.parseInt(strNbr1);
int den = Integer.parseInt(strNbr2);
Rational r = new Rational(num,den);
JOptionPane.showMessageDialog(null,r.getNum()+"/"+r.getDen()+" equals "+r.getDecimal());
System.exit(0);
}
}
class Rational
{
private int num;
private int den;
private double dec;
public Rational(int num, int den){
this.num = num;
this.den = den;
this.dec = dec;
}
public int getNum()
{
return num;
}
public int getDen()
{
return den;
}
public double getDecimal()
{
return dec;
}
private int getGCF(int n1,int n2)
{
int rem = 0;
int gcf = 0;
do
{
rem = n1 % n2;
if (rem == 0)
gcf = n2;
else
{
n1 = n2;
n2 = rem;
}
}
while (rem != 0);
return gcf;
}
}