# Help with a looping issue for homework

• September 16th, 2010, 12:19 PM
constancez
Help with a looping issue for homework
My homework assignment asks us to "I have a question related to homework problem. I am using Netbeans as my IDE. To help understand the specific requirements I will post the assignment criteria
" Write the program in Java (with a graphical user interface) and have it calculate and display the mortgage payment amount from user input of the amount of the mortgage and the user's selection from a menu of available mortgage loans:
- 7 years at 5.35%
- 15 years at 5.5%
- 30 years at 5.75%
Use an array for the mortgage data for the different loans. Display the mortgage payment amount followed by the loan balance and interest paid for each payment over the term of the loan. Allow the user to loop back and enter a new amount and make a new selection or quit. Please insert comments in the program to document the program."

I have had a few issues within the program but I believe I have corrected most. Did I structure my code wrong?

Code Java:

```private void jCalculateButtonActionPerformed(java.awt.event.ActionEvent evt) { // Create an array for the interest rate double[] interestRate = new double[3]; interestRate[0] = 5.35; interestRate[1] = 5.5; interestRate[2] = 5.75;       // Create an array for the number of years int[] numberOfYears = new int[3]; numberOfYears[0] = 7; numberOfYears[1] = 15; numberOfYears[2] = 30; {   // Define varibles   int X = RateTerm.getSelectedIndex(); while (X<3) {   double P = 200000; //Principle double R = 0.0; //monthly interest rate double M = 0.0; // Number of months for the loan double N = 0.0; //Monthly Payment   //create object to convert values to dollars and cents DecimalFormat cash = new DecimalFormat("\$0.00");   //calculate monthly interest rate R = interestRate[X] / (12 * 100);   //calculate total number of months for loan M = numberOfYears[X] * 12;   //Calculate Monthly Payment N = (P * (R * (Math.pow((1 + R), M)) / (Math.pow((1 + R), M) - 1)));   this.jTxtMonthlyPayment.setText(+ interestRate[X] + "% for " + numberOfYears[X] + " years is "+ cash.format(N)); X = X + 1;   }```
• September 16th, 2010, 01:42 PM
Brt93yoda
Re: Help with a looping issue for homework
Well, can you post ALL of your code. I can see a problem right away, you have 3 opening brackets, but only 1 closing.
Code java:

``` int[] numberOfYears = new int[3]; numberOfYears[0] = 7; numberOfYears[1] = 15; numberOfYears[2] = 30; { // whats up with this line?```
• September 19th, 2010, 11:59 AM
constancez
Re: Help with a looping issue for homework
Here is my whole code

Code Java:

``` import java.text.DecimalFormat; import javax.swing.JComboBox;   /* * To change this template, choose Tools | Templates * and open the template in the editor. */   /* * GUIForm.java * * Created on Sep 14, 2010, 7:16:57 AM */ /** * * @author Connie's Desktop */ public class GUIForm extends javax.swing.JFrame {   private static class RateTerm {     public RateTerm() {   } }   /** Creates new form GUIForm */ public GUIForm() { initComponents(); }   /** This method is called from within the constructor to * initialize the form. * WARNING: Do NOT modify this code. The content of this method is * always regenerated by the Form Editor. */ @SuppressWarnings("unchecked") // <editor-fold defaultstate="collapsed" desc="Generated Code"> private void initComponents() {   jPrincipleLabel = new javax.swing.JLabel(); RateTerm = new javax.swing.JComboBox(); jTxtPrinciple = new javax.swing.JTextField(); jPaymentLabel = new javax.swing.JLabel(); jCalculateButton = new javax.swing.JButton(); jClearButton = new javax.swing.JButton(); jExitButton = new javax.swing.JButton(); jScrollPane1 = new javax.swing.JScrollPane(); jTxtMonthlyPayment = new javax.swing.JTextArea();   setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); setTitle("Mortgage Calculator");   jPrincipleLabel.setText("Principle");   RateTerm.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "7 Years at 5.35%", "15 Years at 5.5%", "30 Years at 5.75%" })); RateTerm.setBorder(new javax.swing.border.MatteBorder(null)); RateTerm.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { RateTermActionPerformed(evt); } });   jTxtPrinciple.setBorder(null); jTxtPrinciple.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jTxtPrincipleActionPerformed(evt); } });   jPaymentLabel.setText("Monthly Payment");   jCalculateButton.setText("Calculate"); jCalculateButton.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jCalculateButtonActionPerformed(evt); } });   jClearButton.setText("Clear"); jClearButton.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jClearButtonActionPerformed(evt); } });   jExitButton.setText("Exit"); jExitButton.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jExitButtonActionPerformed(evt); } });   jTxtMonthlyPayment.setColumns(20); jTxtMonthlyPayment.setRows(5); jTxtMonthlyPayment.setBorder(null); jScrollPane1.setViewportView(jTxtMonthlyPayment);   javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addGap(18, 18, 18) .addComponent(jPrincipleLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 69, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGroup(layout.createSequentialGroup() .addContainerGap() .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jPaymentLabel) .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 260, javax.swing.GroupLayout.PREFERRED_SIZE) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false) .addComponent(RateTerm, javax.swing.GroupLayout.Alignment.LEADING, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(jTxtPrinciple, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 139, Short.MAX_VALUE))) .addGap(24, 24, 24) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING) .addComponent(jExitButton) .addComponent(jClearButton) .addComponent(jCalculateButton)))) .addContainerGap(54, Short.MAX_VALUE)) ); layout.setVerticalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addGap(32, 32, 32) .addComponent(jPrincipleLabel) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(jTxtPrinciple, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(22, 22, 22) .addComponent(RateTerm, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(27, 27, 27) .addComponent(jPaymentLabel) .addGap(18, 18, 18) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING) .addGroup(layout.createSequentialGroup() .addComponent(jCalculateButton) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 19, Short.MAX_VALUE) .addComponent(jClearButton) .addGap(28, 28, 28) .addComponent(jExitButton)) .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 116, Short.MAX_VALUE)) .addGap(26, 26, 26)) );   pack(); }// </editor-fold>   private void RateTermActionPerformed(java.awt.event.ActionEvent evt) {   //Declare variables String firstString = "7 Years at 5.35%"; String secondString = "15 Years at 5.5%"; String thirdString = "30 Years at 5.75%";     }   private void jCalculateButtonActionPerformed(java.awt.event.ActionEvent evt) { // Create an array for the interest rate double[] interestRate = new double[3]; interestRate[0] = 5.35; interestRate[1] = 5.5; interestRate[2] = 5.75;       // Create an array for the number of years int[] numberOfYears = new int[3]; numberOfYears[0] = 7; numberOfYears[1] = 15; numberOfYears[2] = 30; {   // Define varibles   int X = RateTerm.getSelectedIndex(); while (X<3) {   double P = 200000; //Principle double R = 0.0; //monthly interest rate double M = 0.0; // Number of months for the loan double N = 0.0; //Monthly Payment   //create object to convert values to dollars and cents DecimalFormat cash = new DecimalFormat("\$0.00");   //calculate monthly interest rate R = interestRate[X] / (12 * 100);   //calculate total number of months for loan M = numberOfYears[X] * 12;   //Calculate Monthly Payment N = (P * (R * (Math.pow((1 + R), M)) / (Math.pow((1 + R), M) - 1)));   this.jTxtMonthlyPayment.setText(+ interestRate[X] + "% for " + numberOfYears[X] + " years is "+ cash.format(N)); X = X + 1;   }   } }   private void jClearButtonActionPerformed(java.awt.event.ActionEvent evt) { // making the clear button function jTxtPrinciple.setText(""); jTxtMonthlyPayment.setText(""); } public JComboBox getRateTerm() { return RateTerm; }   public void setRateTerm(JComboBox RateTerm) { this.RateTerm = RateTerm;}     private void jExitButtonActionPerformed(java.awt.event.ActionEvent evt) { System.exit(0); }   private void jTxtPrincipleActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: }   /** * @param args the command line arguments */ public static void main(String args[]) { java.awt.EventQueue.invokeLater(new Runnable() {   public void run() { new GUIForm().setVisible(true); } }); } // Variables declaration - do not modify private javax.swing.JComboBox RateTerm; private javax.swing.JButton jCalculateButton; private javax.swing.JButton jClearButton; private javax.swing.JButton jExitButton; private javax.swing.JLabel jPaymentLabel; private javax.swing.JLabel jPrincipleLabel; private javax.swing.JScrollPane jScrollPane1; private javax.swing.JTextArea jTxtMonthlyPayment; private javax.swing.JTextField jTxtPrinciple; // End of variables declaration }```
• September 19th, 2010, 01:09 PM
Norm
Re: Help with a looping issue for homework
Have you solved all your issues?
• September 20th, 2010, 12:49 AM
constancez
Re: Help with a looping issue for homework
No I haven't in fact I have made them a bit worse I think. I added a loop based off of an early assignment we had that did not use a GUI. but cannot figure out how to make it conform to a GUI. Any help you can give me to help me conform this to the assignment. Let me also add this is last weeks assignment that i have already received a filing grade for but my instructor hasn't gotten back to me yet on any of my questions. Here is my most recent updated code.

Code Java:

```import java.text.DecimalFormat; import javax.swing.JComboBox;   /* * To change this template, choose Tools | Templates * and open the template in the editor. */   /* * GUIForm.java * * Created on Sep 14, 2010, 7:16:57 AM */ /** * * @author Connie's Desktop */ public class GUIForm extends javax.swing.JFrame {   private static class RateTerm {   public RateTerm() { } }   /** Creates new form GUIForm */ public GUIForm() { initComponents(); }   /** This method is called from within the constructor to * initialize the form. * WARNING: Do NOT modify this code. The content of this method is * always regenerated by the Form Editor. */ @SuppressWarnings("unchecked") // <editor-fold defaultstate="collapsed" desc="Generated Code"> private void initComponents() {   jPrincipleLabel = new javax.swing.JLabel(); RateTerm = new javax.swing.JComboBox(); jTxtPrinciple = new javax.swing.JTextField(); jPaymentLabel = new javax.swing.JLabel(); jCalculateButton = new javax.swing.JButton(); jClearButton = new javax.swing.JButton(); jExitButton = new javax.swing.JButton(); jScrollPane1 = new javax.swing.JScrollPane(); jTxtMonthlyPayment = new javax.swing.JTextArea();   setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); setTitle("Mortgage Calculator");   jPrincipleLabel.setText("Principle");   RateTerm.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "7 Years at 5.35%", "15 Years at 5.5%", "30 Years at 5.75%" })); RateTerm.setBorder(new javax.swing.border.MatteBorder(null)); RateTerm.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { RateTermActionPerformed(evt); } });   jTxtPrinciple.setBorder(null); jTxtPrinciple.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jTxtPrincipleActionPerformed(evt); } });   jPaymentLabel.setText("Monthly Payment");   jCalculateButton.setText("Calculate"); jCalculateButton.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jCalculateButtonActionPerformed(evt); } });   jClearButton.setText("Clear"); jClearButton.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jClearButtonActionPerformed(evt); } });   jExitButton.setText("Exit"); jExitButton.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jExitButtonActionPerformed(evt); } });   jTxtMonthlyPayment.setColumns(20); jTxtMonthlyPayment.setRows(5); jTxtMonthlyPayment.setBorder(null); jScrollPane1.setViewportView(jTxtMonthlyPayment);   javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addGap(18, 18, 18) .addComponent(jPrincipleLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 69, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGroup(layout.createSequentialGroup() .addContainerGap() .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jPaymentLabel) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false) .addComponent(RateTerm, javax.swing.GroupLayout.Alignment.LEADING, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(jTxtPrinciple, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 139, Short.MAX_VALUE)) .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 417, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGap(15, 15, 15) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING) .addComponent(jExitButton) .addGroup(layout.createSequentialGroup() .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(jCalculateButton)) .addComponent(jClearButton))) ); layout.setVerticalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addGap(32, 32, 32) .addComponent(jPrincipleLabel) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(jTxtPrinciple, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(22, 22, 22) .addComponent(RateTerm, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(27, 27, 27) .addComponent(jPaymentLabel) .addGap(18, 18, 18) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addComponent(jCalculateButton) .addGap(18, 18, 18) .addComponent(jClearButton) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 44, Short.MAX_VALUE) .addComponent(jExitButton)) .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 131, Short.MAX_VALUE)) .addContainerGap()) );   pack(); }// </editor-fold>   private void RateTermActionPerformed(java.awt.event.ActionEvent evt) {   //Declare variables String firstString = "7 Years at 5.35%"; String secondString = "15 Years at 5.5%"; String thirdString = "30 Years at 5.75%";     }   private void jCalculateButtonActionPerformed(java.awt.event.ActionEvent evt) { // Create an array for the interest rate double[] interestRate = new double[3]; interestRate[0] = 5.35; interestRate[1] = 5.5; interestRate[2] = 5.75;       // Create an array for the number of years int[] numberOfYears = new int[3]; numberOfYears[0] = 7; numberOfYears[1] = 15; numberOfYears[2] = 30; {   // Define varibles   int X = RateTerm.getSelectedIndex(); {   double P = 200000; //Principle double R = 0.0; //monthly interest rate double M = 0.0; // Number of months for the loan double N = 0.0; //Monthly Payment   //create object to convert values to dollars and cents DecimalFormat cash = new DecimalFormat("\$0.00");   //calculate monthly interest rate R = interestRate[X] / (12 * 100);   //calculate total number of months for loan M = numberOfYears[X] * 12;     //Calculate Monthly Payment N = (P * (R * (Math.pow((1 + R), M)) / (Math.pow((1 + R), M) - 1)));   //this.jTxtMonthlyPayment.setText(+ interestRate[X] + "% for " + numberOfYears[X] + " years is "+ cash.format(N));     // Create a loop which displays monthly payment   while (M > 0) {       //interest payment = IP, then calculate monthly interest payment double IP = P * R;       //Principle Payment = PP, then calculate monthly principal payment double PP = N - IP;     //calculate remaining principal balance P = P - PP;         // Amortization print out //System.out.println("Number of payments" + M + "Monthly Payment= " + cash.format(N) + "Interest Payment= " + cash.format(IP) + "Mortgage Balance= " + cash.format(P)); jTxtMonthlyPayment.setText("Number of payments=" + M + "Monthly Payment= " + cash.format(N) + "Interest Payment= " + cash.format(IP) + "Mortgage Balance= " + cash.format(P));       //count down number for the number of payments left M = M - 1;           //end nested while loop }                                     }                     } }   private void jClearButtonActionPerformed(java.awt.event.ActionEvent evt) { // making the clear button function jTxtPrinciple.setText(""); jTxtMonthlyPayment.setText(""); } public JComboBox getRateTerm() { return RateTerm; }   public void setRateTerm(JComboBox RateTerm) { this.RateTerm = RateTerm; }   private void jExitButtonActionPerformed(java.awt.event.ActionEvent evt) { System.exit(0); }   private void jTxtPrincipleActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: }   /** * @param args the command line arguments */ public static void main(String args[]) { java.awt.EventQueue.invokeLater(new Runnable() {   public void run() { new GUIForm().setVisible(true); } }); } // Variables declaration - do not modify private javax.swing.JComboBox RateTerm; private javax.swing.JButton jCalculateButton; private javax.swing.JButton jClearButton; private javax.swing.JButton jExitButton; private javax.swing.JLabel jPaymentLabel; private javax.swing.JLabel jPrincipleLabel; private javax.swing.JScrollPane jScrollPane1; private javax.swing.JTextArea jTxtMonthlyPayment; private javax.swing.JTextField jTxtPrinciple; // End of variables declaration }```
• September 20th, 2010, 08:32 AM
Norm
Re: Help with a looping issue for homework
Quote:

cannot figure out how to make it conform to a GUI
Can you explain what your problem is?