I need to display the loan table with the annual interest rate, monthly payment, and total payment for the number of years entered. When I run my program, it only displays the info for 8%, when I need to display all interest rates from 5% to 8% in increments of 1/8%.

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;

public class Ch1613 extends JFrame implements ActionListener

{
	private JLabel jlbAmount = new JLabel("Loan Amount");
	private JTextField jtfAmount = new JTextField(8);
	private JLabel jlbYears = new JLabel("Number of Years");
	private JTextField jtfYears = new JTextField(2);
	private JTextArea jtaTable = new JTextArea("Interest Rate" + "\t" + "Monthly Payment" + "\t\t" + "Total Payment");
	private JButton showTable = new JButton("Show Table");
	private JScrollPane scrollPane = new JScrollPane();
	
public Ch1613()
{
	
	JPanel p = new JPanel();
	p.add(jlbAmount);
	p.add(jtfAmount);
	p.add(jlbYears);
	p.add(jtfYears);
	p.add(showTable);
	
	
	
	
	
	
	add(p, BorderLayout.NORTH);
	add(scrollPane, BorderLayout.CENTER);
	scrollPane.getViewport().add(jtaTable, null);
	showTable.addActionListener(this);
	
}
  // Main method

  public static void main(String[] args) 

 {

	  JFrame frame = new Ch1613();
		frame.pack();
		frame.setTitle("Exercise16_13");
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setLocationRelativeTo(null);
		frame.setVisible(true);
	  
		

	  
   
 }
    
  
  public void actionPerformed(ActionEvent e)
	{
		double amount = Double.valueOf(jtfAmount.getText().trim()).doubleValue();
		
		int years = Integer.valueOf(jtfYears.getText().trim()).intValue();
		if (e.getSource() == showTable)
		{
			
			
			double monthlyInterestRate;

		    double monthlyPayment;

		    double totalPayment;

		 

		    for (double annualInterestRate = 5.0; annualInterestRate <= 8.0;

		      annualInterestRate += 1.0 / 8) 

		   {

		      // Obtain monthly interest rate

		      monthlyInterestRate = annualInterestRate / 1200;

		 

		      // Compute mortgage

		      monthlyPayment = amount * monthlyInterestRate /

		        (1 - (Math.pow(1 / (1 + monthlyInterestRate), years * 12)));

		      totalPayment = monthlyPayment * years * 12;

		 

		      // Display results
		   // Display the header

		      
		   


		      jtaTable.setText((int)annualInterestRate + "%" + ("\t\t" + (int)(monthlyPayment * 100) / 100.0) + "\t\t\t" + (int)(totalPayment * 100) / 100.0);
		     
		      
		      

		    }

		  }
		}
	}

Nevermind, I figured it out.

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;

public class Ch1613 extends JFrame implements ActionListener

{
    private JLabel jlbAmount = new JLabel("Loan Amount");
    private JTextField jtfAmount = new JTextField(8);
    private JLabel jlbYears = new JLabel("Number of Years");
    private JTextField jtfYears = new JTextField(2);
    private JTextArea jtaTable = new JTextArea(30, 35);
    private JButton showTable = new JButton("Show Table");
    private JScrollPane scrollPane = new JScrollPane();

public Ch1613()
{

    JPanel p = new JPanel();
    p.add(jlbAmount);
    p.add(jtfAmount);
    p.add(jlbYears);
    p.add(jtfYears);
    p.add(showTable);






    add(p, BorderLayout.NORTH);
    add(scrollPane, BorderLayout.CENTER);
    scrollPane.getViewport().add(jtaTable, null);
    showTable.addActionListener(this);

}
  // Main method

  public static void main(String[] args) 

 {

      JFrame frame = new Ch1613();
        frame.pack();
        frame.setTitle("Exercise16_13");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);





 }


  public void actionPerformed(ActionEvent e)
    {
        double amount = Double.valueOf(jtfAmount.getText().trim()).doubleValue();

        int years = Integer.valueOf(jtfYears.getText().trim()).intValue();
        if (e.getSource() == showTable)
        {


            double monthlyInterestRate;

            double monthlyPayment;

            double totalPayment;



            for (double annualInterestRate = 5.0; annualInterestRate <= 8.0;

              annualInterestRate += 1.0 / 8) 

           {

              // Obtain monthly interest rate

              monthlyInterestRate = annualInterestRate / 1200;



              // Compute mortgage

              monthlyPayment = amount * monthlyInterestRate /

                (1 - (Math.pow(1 / (1 + monthlyInterestRate), years * 12)));

              totalPayment = monthlyPayment * years * 12;



              // Display results


              jtaTable.append("Interest Rate" + "\t" + "Monthly Payment" + "\t\t" + "Total Payment" + "\n" +annualInterestRate + "%" + ("\t" + (int)(monthlyPayment * 100) / 100.0) + "\t\t\t" + (int)(totalPayment * 100) / 100.0);




            }

          }
        }
    }
[QUOTE=george21;1012128]import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;

public class Ch1613 extends JFrame implements ActionListener

{
	private JLabel jlbAmount = new JLabel("Loan Amount");
	private JTextField jtfAmount = new JTextField(8);
	private JLabel jlbYears = new JLabel("Number of Years");
	private JTextField jtfYears = new JTextField(2);
	private JTextArea jtaTable = new JTextArea(30, 35);
	private JButton showTable = new JButton("Show Table");
	private JScrollPane scrollPane = new JScrollPane();
	
public Ch1613()
{
	
	JPanel p = new JPanel();
	p.add(jlbAmount);
	p.add(jtfAmount);
	p.add(jlbYears);
	p.add(jtfYears);
	p.add(showTable);
	
	
	
	
	
	
	add(p, BorderLayout.NORTH);
	add(scrollPane, BorderLayout.CENTER);
	scrollPane.getViewport().add(jtaTable, null);
	showTable.addActionListener(this);
	
}
  // Main method

  public static void main(String[] args) 

 {

	  JFrame frame = new Ch1613();
		frame.pack();
		frame.setTitle("Exercise16_13");
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setLocationRelativeTo(null);
		frame.setVisible(true);
	  
		

	  
   
 }
      
  
  public void actionPerformed(ActionEvent e)
	{
		double amount = Double.valueOf(jtfAmount.getText().trim()).doubleValue();
		
		int years = Integer.valueOf(jtfYears.getText().trim()).intValue();
		if (e.getSource() == showTable)
		{
			
			
			double monthlyInterestRate;

		    double monthlyPayment;

		    double totalPayment;

		 

		    for (double annualInterestRate = 5.0; annualInterestRate <= 8.0;

		      annualInterestRate += 1.0 / 8) 

		   {

		      // Obtain monthly interest rate

		      monthlyInterestRate = annualInterestRate / 1200;

		 

		      // Compute mortgage

		      monthlyPayment = amount * monthlyInterestRate /

		        (1 - (Math.pow(1 / (1 + monthlyInterestRate), years * 12)));

		      totalPayment = monthlyPayment * years * 12;

		 

		      // Display results
		        
		      
		      jtaTable.append("Interest Rate" + "\t" + "Monthly Payment" + "\t\t" + "Total Payment" + "\n" +annualInterestRate + "%" + ("\t" + (int)(monthlyPayment * 100) / 100.0) + "\t\t\t" + (int)(totalPayment * 100) / 100.0);
		     
		      
		      

		    }

		  }
		}
	}

And what is your question? You said you can't figure out how to display the header only once. What do you mean by that? Because you said 'header' on the one hand, on the other hand, the only area where you are really displaying stuff is the JTextArea, but that is not a header. . please clarify your question & why you think it isn't working, what you want it to do, etc.

I need a Header for the JTextArea like:

"Interest Rate" + "\t" + "Monthly Payment" + "\t\t" + "Total Payment"

I don't know where to place it or how to write it so it shows at the top of JTextArea only once. I tried adding it to the append, but that doesn't work.

It doesn't work because you put it inside of a for loop. Meaning that every time the for loop is executed, it gets appended however many times the loop iterates. Since you only want it appended once, you would need to put the append statement before the for loop. You would also need to take out the extra stuff, and make it only say

jtaTable.append("Interest Rate" + "\t" + "Monthly Payment" + "\t\t" + "Total Payment" + "\n");

So essentially, that will give you a header that has three columns: Interest Rate, Monthly Payment, and Total Payment.


Oh, and one more thing: You also need to clear the text area when the button is clicked. So use jtaTable.setText(""); at the beginning of your logic inside the actionPerformed method. Otherwise, every time you calculate a different loan amount, it will still have the previous calculation shown in the JTextArea.

After the changes I've suggested, your code would look something like this:

if (e.getSource() == showTable)
		{
			//Clear the text area of previous calculations
                         jtaTable.setText("");
			
			double monthlyInterestRate;

		    double monthlyPayment;

		    double totalPayment;

		 
		      jtaTable.append("Interest Rate" + "\t" + "Monthly Payment" + "\t" + "Total Payment" + "\n");

		    for (double annualInterestRate = 5.0; annualInterestRate <= 8.0;

		      annualInterestRate += 1.0 / 8) 

		   {

		      // Obtain monthly interest rate

		      monthlyInterestRate = annualInterestRate / 1200;

		 

		      // Compute mortgage

		      monthlyPayment = amount * monthlyInterestRate /

		        (1 - (Math.pow(1 / (1 + monthlyInterestRate), years * 12)));

		      totalPayment = monthlyPayment * years * 12;

		 

		      // Display results
		        
		      		     
		      
		      

		    }

Notice that I intentionally did not add in the code to perform the calculations and correctly align them in the text area for you - you'll have to figure that out on your own. Keep in mind that using "append" will simply add the text onto the end of whatever other text is in the JTextArea. So you'll have to use newlines appropriately to create different rows. Also, one final note - try not to name text areas "table". It is confusing.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.