Calculation always comes out zero


Last Updated:

  1. gotenks05

    gotenks05 Member This Topic's Starter

    Joined:
    May 5, 2010
    Messages:
    8
    Likes Received:
    0
    I am having some troubles with a program that I am trying to create. The program is supposed to do RMD calculations based on the age of the person, which is calculated by their given birth date, and the account balance, which is user supplied. My problem is that the results always come out as 0.0. The birth date is being accepted, since the age is being calculated and the balance is also accepted. How do I fix this?

    The activity code looks like this:

    Code (Text):
    1. package com.MinDis.android;
    2.  
    3. import android.app.Activity;
    4. import android.os.Bundle;
    5. import android.view.*;
    6. import android.widget.*;
    7.  
    8. public class MinDis extends Activity
    9. {
    10.     String display;
    11.     EditText date;
    12.     EditText rmd;
    13.     EditText balance;
    14.     Button calc;
    15.     RMD comp = new RMD();
    16.     /** Called when the activity is first created. */
    17.     @Override
    18.     public void onCreate(Bundle savedInstanceState)
    19.     {
    20.         super.onCreate(savedInstanceState);
    21.         setContentView(R.layout.main);
    22.  
    23.     date = (EditText)findViewById(R.id.date);
    24.     balance = (EditText)findViewById(R.id.amount);
    25.     rmd = (EditText)findViewById(R.id.rmd);
    26.     calc = (Button)findViewById(R.id.calculate);
    27.     rmd.setEnabled(false);
    28.     }
    29.  
    30.     public void rmdAction(View v) throws Exception
    31.     {
    32.         String day;
    33.         String amount;
    34.  
    35.         amount = balance.getText().toString();
    36.         day = date.getText().toString();
    37.  
    38.         comp.setBDate(day);
    39.         comp.setBalance(Double.parseDouble(amount));
    40.         rmd.setText(Double.toString(comp.getRMD()));
    41.  
    42.     }
    43. }
    44.  
    It could be my problem, but I do not think it is.

    This is my external class file, which I think is where the problem lies:

    Code (Text):
    1. package com.MinDis.android;
    2.  
    3. import java.util.*;
    4. import java.text.*;
    5.  
    6. public class RMD
    7. {
    8.     double balance;
    9.     double rmd;
    10.     long age;
    11.     String bdate;
    12.     SimpleDateFormat sd = new SimpleDateFormat("MM/dd/yyyy");
    13.    
    14.     Date current = new Date();
    15.     public void setBalance(double i)
    16.     {
    17.         balance = i;
    18.     }
    19.  
    20.     public double getBalance()
    21.     {
    22.         return balance;
    23.     }
    24.  
    25.     public void setBDate(String h)
    26.     {
    27.         bdate = h;
    28.     }
    29.  
    30.     public long getAge() throws Exception
    31.     {
    32.        
    33.         Date birthd = sd.parse(bdate);
    34.         long cur;
    35.         long birth;
    36.         long diff;
    37.  
    38.         cur = current.getTime();
    39.         birth = birthd.getTime();
    40.         diff = cur - birth;
    41.         age = (diff/(24*60*60*1000))/365;
    42.         return age;
    43.     }
    44.  
    45.     public double getRMD()
    46.     {
    47.  
    48.         if (age == 70)
    49.         {
    50.             rmd = balance/27.4;
    51.         }
    52.  
    53.         if (age == 71)
    54.         {
    55.             rmd = balance/26.5;
    56.         }
    57.  
    58.         if (age == 72)
    59.         {
    60.             rmd = balance/25.6;
    61.         }
    62.  
    63.         if (age == 73)
    64.         {
    65.             rmd = balance/24.7;
    66.         }
    67.  
    68.         if (age == 74)
    69.         {
    70.             rmd = balance/23.8;
    71.         }
    72.  
    73.         return rmd;
    74.     }
    75. }
    I have tried using the compareTo() and equals() methods, but it results in either the same or the compiler thinks I am dereferencing a long.
     

    Advertisement
  2. miXer

    miXer Well-Known Member

    Joined:
    Aug 12, 2010
    Messages:
    313
    Likes Received:
    51
    Have you tried debugging it? That always helps me when i dont know where a problem is occuring.
     
  3. gotenks05

    gotenks05 Member This Topic's Starter

    Joined:
    May 5, 2010
    Messages:
    8
    Likes Received:
    0
    I might try that. Also, I implemented the external class with a J2SE program and it gives the same results, so this is not limited to the Android platform, so I have 100% confirmation that my external class is where the problem is.

    Update: I solved the problem.
     

Share This Page

Loading...