Results 1 to 2 of 2
  1. #1
    Anime101 is offline Member
    Join Date
    May 2008
    Posts
    3
    Rep Power
    0

    Default help with income to debt ratio calculator

    Hello, Im new to Java Programming, and Im trying to create a Web Applet of a Income to Debt Ratio calculator. I think Im missing a Math code, because when i run the applet everything works except the when I click calculate it says my debt to ratio income is always 0. Any advice would be greatly appreciated, thanks.


    import java.applet.*;
    import java.awt.*;
    import java.awt.event.*;

    public class DebtRatioIncome extends Applet implements ActionListener
    {
    // declare variables
    Image logo; //declare an Image object
    int Mortgage,AutoLoan,OtherDebt,MonthlyIncome;
    double monthlyIncome,mortgage,autoLoan,otherDebt,ratio, index;

    // construct components
    Label companyLabel = new Label("INCOME TO DEBT RATIO CALCULATOR");
    Label monthlyIncomeLabel= new Label("Enter your Monthly Income here:");
    TextField monthlyIncomeField = new TextField(10);
    Label mortgageLabel = new Label("Enter your Mortgage Payment here: ");
    TextField mortgageField = new TextField(10);
    Label autoLoanLabel = new Label ("Enter your Auto Loan payment here: ");
    TextField autoLoanField = new TextField(10);
    Label otherDebtLabel = new Label ("Enter any Other Debt here:");
    TextField otherDebtField = new TextField(10);
    Button calcButton = new Button("Calculate");
    Label outputLabel = new Label("Click the Calculate button to see your Debt to Income Ratio.");

    public void init()
    {
    setForeground(Color.red);
    add(companyLabel);
    add(mortgageLabel);
    add(mortgageField);
    add(autoLoanLabel);
    add(autoLoanField);
    add(otherDebtLabel);
    add(otherDebtField);
    add(monthlyIncomeLabel);
    add(monthlyIncomeField);
    add(calcButton);
    calcButton.addActionListener(this);
    add(outputLabel);
    logo = getImage(getDocumentBase(), "logo.gif");
    }

    public void actionPerformed(ActionEvent e)
    {

    Mortgage = Integer.parseInt(mortgageField.getText());
    AutoLoan = Integer.parseInt(autoLoanField.getText());
    OtherDebt = Integer.parseInt(otherDebtField.getText());
    MonthlyIncome = Integer.parseInt(monthlyIncomeField.getText());

    ratio=(mortgage+autoLoan+otherDebt)/monthlyIncome;

    outputLabel.setText("YOUR DEBT TO INCOME RATIO IS" + Math.round(index) + ".");
    }

    public void paint(Graphics g)
    {
    g.drawImage(logo,125,160,this);
    }
    }

  2. #2
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    8

    Default

    Java Code:
    Mortgage = Integer.parseInt(mortgageField.getText());
    AutoLoan = Integer.parseInt(autoLoanField.getText());
    OtherDebt = Integer.parseInt(otherDebtField.getText());
    MonthlyIncome = Integer.parseInt(monthlyIncomeField.getText());
    // This (ratio) is declared as a [i]double[/i].
    // Using [i]int[/i]s will round everything off - fractions
    // will be rounded to zero.
    ratio=(mortgage+autoLoan+otherDebt)/monthlyIncome;
    // You can either parse the values above as [i]double[/i]s
    // or you can do something like:
    ratio=(double)(mortgage+autoLoan+otherDebt)/monthlyIncome;
    // to get a double value/fraction.

Similar Threads

  1. Java Calculator
    By aapanju in forum New To Java
    Replies: 3
    Last Post: 04-17-2008, 05:33 AM
  2. Java calculator decimal
    By cart1443 in forum New To Java
    Replies: 2
    Last Post: 04-16-2008, 01:19 PM
  3. calculator not working
    By Renegade85 in forum New To Java
    Replies: 5
    Last Post: 03-10-2008, 03:27 PM
  4. Create a Calculator in Java
    By Albert in forum New To Java
    Replies: 2
    Last Post: 07-04-2007, 08:01 AM
  5. Swing Calculator
    By nemo in forum AWT / Swing
    Replies: 1
    Last Post: 05-28-2007, 11:07 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •