Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 283
  • Last Modified:

display multiple outputs in applet

I'm sure this is simple - but I cannot seem to resolve it, or find the right answer.  I have a jave applet project to calculate a car loan.  I have it done and it is working EXCEPT....
Each calculation is correct and displays if I comment out the other 2.
   When I leave all 3 - just the last output shows...
I need all 3 output lines to show on my project... I've tried some different append syntax - but not finding the right ones...get nothing but errors.
I'd sure appreciate any recommendations ...thanks in advance!
Here's my code:

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

public class carLoanApplet2ND extends Applet implements ActionListener   //action is ActionListener - extends means to expect

  Label Title = new Label("Car Loan Calculator"); //title is the variable-name it anything
   Label costCarLabel = new Label ("Please enter the Price of the Car:"); //this is a prompt label
   TextField costCarField = new TextField(25); //this is a text box -25 characters
   Label downPaymentLabel = new Label ("Please enter the down payment:"); //this is another prompt
   TextField downPaymentField = new TextField(25); //text field to get data
   Label loanPeriodLabel = new Label ("Please enter the Loan Period in Years:"); //this is another prompt
   TextField loanPeriodField = new TextField(25); //text field to get data

   Button calcButton = new Button("Calculate Car Payment");  //button name label is in " "
   Label outputLabel = new Label ("Enter the data and then click the button");

   public void init()
      // Set Background color to cyan
      add(Title); // display title
      add(costCarLabel);  //display label

      calcButton.addActionListener(this);  //add button that states action performed - calls method

   public void actionPerformed(ActionEvent e)  //this is calculation method
    double costCar, downPayment, loanPeriod, loan, interest, payment;  //pass values
    //Converting input to values
    costCar =(double)Integer.parseInt(costCarField.getText()); //convert to value
    downPayment = (double)Integer.parseInt(downPaymentField.getText());
    loanPeriod = (double)Integer.parseInt(loanPeriodField.getText());

    //Calculation amount of loan
    loan = costCar - downPayment;

     //Calc interest
     if (loan < 5000)
        interest = .089;
        if (loan < 7500)
            interest = .079;
            if (loan < 8500)
                interest = .069;
                  if (loan < 10000)
                           interest = .059;
                                interest = .039;

    //Calculate payment
    payment = ((loan*interest) + loan) / (loanPeriod*12);

    //Output loan amount
    outputLabel.setText ("The Loan Amount =  $" + loan);

    //Output interest
    outputLabel.setText ("The Interest Rate is = " + interest);

    //Output payment
    outputLabel.setText ("The Loan Payment is =  $" + Math.round(payment));


1 Solution
Why are you using a Label to show the output ?

If uou have to use a labelmthen why not use three position one under the other , and do :

outputLabel2.setText ("The Loan Amount =  $" + loan);
outputLabel2.setText ("The Interest Rate is = " + interest);
outputLabel3.setText ("The Loan Payment is =  $" + Math.round(payment));
You are outputting results to the same label, so each overwrites the previous.
Either use 3 different labels as suggested by ozy, or use a TextArea with 3 lines and append each result on a new line.
vonbAuthor Commented:
Thanks -
The Label to show output was part of the assignment...
Thanks -You so much - I should have seen that, especially since only my last output was appearing....
thanks for helping me out!

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now