Page 2 of 2 FirstFirst 12
Results 21 to 22 of 22
  1. #21
    bamagirl31 is offline Member
    Join Date
    May 2011
    Posts
    47
    Rep Power
    0

    Default RE::

    Hi, I want to almost throw my computer at something by now. Anyway, my program does run, but it does not get the values from reading the text file when the menu items are clicked under File. The values from the read text file are those of strLine and then I changed it to intRates.
    Please help. Thanks.
    Java Code:
    package mortgage;
    
    
    
    /**
     *
     * @author Lekeisha
     */
    import java.util.Scanner;
    import java.io.*;
    import java.util.logging.Level;
    import java.util.logging.Logger;
    
    public class MortgageGUI extends javax.swing.JFrame 
    {
        
               
        /** Creates new form MortgageGUI */
        public MortgageGUI() {
            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() {
    
            jPanel1 = new javax.swing.JPanel();
            mortgageTextField1 = new javax.swing.JTextField();
            loantermTextField2 = new javax.swing.JTextField();
            interestrateTextField3 = new javax.swing.JTextField();
            mortgagecalculatedTextField4 = new javax.swing.JTextField();
            loancalculatedTextField5 = new javax.swing.JTextField();
            interestcalculatedTextField6 = new javax.swing.JTextField();
            mortgageLabel1 = new javax.swing.JLabel();
            loantermLabel3 = new javax.swing.JLabel();
            interestrateLabel5 = new javax.swing.JLabel();
            jLabel7 = new javax.swing.JLabel();
            calculateButton7 = new javax.swing.JButton();
            clearjButton8 = new javax.swing.JButton();
            jLabel9 = new javax.swing.JLabel();
            jLabel11 = new javax.swing.JLabel();
            jMenuBar1 = new javax.swing.JMenuBar();
            jMenu1 = new javax.swing.JMenu();
            jMenuItem1 = new javax.swing.JMenuItem();
            jMenuItem2 = new javax.swing.JMenuItem();
            jMenuItem3 = new javax.swing.JMenuItem();
            jMenu2 = new javax.swing.JMenu();
    
            javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
            jPanel1.setLayout(jPanel1Layout);
            jPanel1Layout.setHorizontalGroup(
                jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGap(0, 100, Short.MAX_VALUE)
            );
            jPanel1Layout.setVerticalGroup(
                jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGap(0, 100, Short.MAX_VALUE)
            );
    
            setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
            setTitle("Mortgage Calculator");
    
            mortgageTextField1.setText("200000");
            mortgageTextField1.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    mortgageTextField1ActionPerformed(evt);
                }
            });
    
            loantermTextField2.setText("30");
            loantermTextField2.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    loantermTextField2ActionPerformed(evt);
                }
            });
    
            interestrateTextField3.setText(".0575");
            interestrateTextField3.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    interestrateTextField3ActionPerformed(evt);
                }
            });
    
            mortgagecalculatedTextField4.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    mortgagecalculatedTextField4ActionPerformed(evt);
                }
            });
    
            loancalculatedTextField5.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    loancalculatedTextField5ActionPerformed(evt);
                }
            });
    
            interestcalculatedTextField6.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    interestcalculatedTextField6ActionPerformed(evt);
                }
            });
    
            mortgageLabel1.setText("Mortgage Amount");
    
            loantermLabel3.setText("Loan Term");
    
            interestrateLabel5.setText("Interest Rate");
    
            jLabel7.setText("Mortgage Amount");
    
            calculateButton7.setText("Calculate");
            calculateButton7.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    calculateButton7ActionPerformed(evt);
                }
            });
    
            clearjButton8.setText("Clear");
            clearjButton8.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    clearjButton8ActionPerformed(evt);
                }
            });
    
            jLabel9.setText("Loan Balance");
    
            jLabel11.setText("Interest Amount");
    
            jMenu1.setText("File");
    
            jMenuItem1.setText("7yrs at 5.35 %");
            jMenuItem1.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    jMenuItem1ActionPerformed(evt);
                }
            });
            jMenu1.add(jMenuItem1);
    
            jMenuItem2.setText("15yrs at 5.5%");
            jMenuItem2.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    jMenuItem2ActionPerformed(evt);
                }
            });
            jMenu1.add(jMenuItem2);
    
            jMenuItem3.setText("30yrs at 5.75%");
            jMenuItem3.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    jMenuItem3ActionPerformed(evt);
                }
            });
            jMenu1.add(jMenuItem3);
    
            jMenuBar1.add(jMenu1);
    
            jMenu2.setText("Edit");
            jMenuBar1.add(jMenu2);
    
            setJMenuBar(jMenuBar1);
    
            javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
            getContentPane().setLayout(layout);
            layout.setHorizontalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addContainerGap(77, Short.MAX_VALUE)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(mortgageLabel1)
                        .addComponent(jLabel7)
                        .addComponent(mortgageTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 67, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(mortgagecalculatedTextField4, javax.swing.GroupLayout.PREFERRED_SIZE, 70, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGap(30, 30, 30)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                        .addComponent(jLabel9, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(loantermLabel3)
                        .addComponent(loantermTextField2)
                        .addComponent(loancalculatedTextField5))
                    .addGap(30, 30, 30)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(jLabel11)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addComponent(interestrateTextField3, javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(interestrateLabel5, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        .addComponent(interestcalculatedTextField6, javax.swing.GroupLayout.PREFERRED_SIZE, 66, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGap(239, 239, 239)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                        .addComponent(calculateButton7, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(clearjButton8, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addGap(108, 108, 108))
            );
            layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addGap(30, 30, 30)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(loantermLabel3)
                                .addComponent(interestrateLabel5)
                                .addComponent(mortgageLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 14, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(loantermTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(interestrateTextField3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(mortgageTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGap(20, 20, 20))
                        .addComponent(calculateButton7, javax.swing.GroupLayout.Alignment.TRAILING))
                    .addGap(40, 40, 40)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabel9)
                        .addComponent(jLabel11)
                        .addComponent(jLabel7))
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(loancalculatedTextField5, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(interestcalculatedTextField6, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(mortgagecalculatedTextField4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addComponent(clearjButton8)
                    .addContainerGap(80, Short.MAX_VALUE))
            );
    
            pack();
        }// </editor-fold>                        
        
            
         
               
           
           
        private void mortgagecalculatedTextField4ActionPerformed(java.awt.event.ActionEvent evt) {                                                             
           System.out.println();
          //calculates the mortgage payment amount based on amount entered by user // TODO add your handling code here:
        }                                                            
     
        private void calculateButton7ActionPerformed(java.awt.event.ActionEvent evt) {                                                 
                 
            
            //initiates the monthly interest rate
            //initiates the mortgage payment
            //initiates interest paid
            //initiates principal paid
            double intRates = Double.parseDouble(interestrateTextField3.getText());
            double monIntRate = intRates/12; //interest rate divided by 12 months equals the monthly interest rate
            double loanTerm = Double.parseDouble(loantermTextField2.getText());
            double totMons = loanTerm*12; //term of loan multiplied by 12 months equals total months of term
            
            double amLoanOne = Double.parseDouble(mortgageTextField1.getText());
            //initiates the amount of loan balance after initial amount
            double monPay = amLoanOne*monIntRate/(1-(Math.pow((1+monIntRate), (-totMons))));
            mortgagecalculatedTextField4.setText(Math.round(monPay) + "");
            //calculates the mortgage payment
            double intsPaid = amLoanOne*monIntRate;
            interestcalculatedTextField6.setText(Math.round(intsPaid) + "");
            //calculates the interest paid
            double princPaid = monPay - intsPaid;
            //calculates the principal paid
            double amLoanNxt = amLoanOne - princPaid;
            loancalculatedTextField5.setText(Math.round(amLoanNxt) + "");
            //calculates the amount of loan balance after initial amount  
           
        }                                                
    
        private void clearjButton8ActionPerformed(java.awt.event.ActionEvent evt) {                                              
          mortgageTextField1.setText("");
          loantermTextField2.setText("");
          interestrateTextField3.setText("");
          mortgagecalculatedTextField4.setText("");
          loancalculatedTextField5.setText("");
          interestcalculatedTextField6.setText("");
          
          
          
          
         // TODO add your handling code here:
        }                                             
    
        private void mortgageTextField1ActionPerformed(java.awt.event.ActionEvent evt) {                                                   
            String amLoanOne = mortgageTextField1.getText(); // TODO add your handling code here:
        }                                                  
    
        private void loantermTextField2ActionPerformed(java.awt.event.ActionEvent evt) {                                                   
        String loanTerm = loantermTextField2.getText();    // TODO add your handling code here:
        }                                                  
    
        private void interestrateTextField3ActionPerformed(java.awt.event.ActionEvent evt) {                                                       
        String intRates = interestrateTextField3.getText();    // TODO add your handling code here:
        }                                                      
    
        private void loancalculatedTextField5ActionPerformed(java.awt.event.ActionEvent evt) {                                                         
       System.out.println();
               
          //calculates the loan balance based on the amount entered by user     // TODO add your handling code here:
        }                                                        
    
        private void interestcalculatedTextField6ActionPerformed(java.awt.event.ActionEvent evt) {                                                             
       System.out.println();
       
      
          //calculates the interest paid with interest rate entered by user     // TODO add your handling code here:
        }                                                            
    
               
           
        
        private void jMenuItem1ActionPerformed(java.awt.event.ActionEvent evt) {                                           
            try
             {
       // Open the file that is the first 
       // command line parameter
       FileInputStream fstream = new FileInputStream("intRates.txt");
       // Get the object of DataInputStream
       DataInputStream in = new DataInputStream(fstream);
       BufferedReader br = new BufferedReader(new InputStreamReader(in));
        String strLine;    
             } catch (FileNotFoundException ex) {
                Logger.getLogger(MortgageGUI.class.getName()).log(Level.SEVERE, null, ex);
            }
             
                   
                    
                    
                    
                    double [] intRates = Double.parseDouble(strLine);
                    double [] intRates = new double[3];
                    for (int i = 0; i < intRates.length; i++)
                    {
                      
    
                    
                    double monIntRate = intRates[0]/12; //interest rate divided by 12 months equals the monthly interest rate
                    double loanTerm = 7.;
                    double totMons = loanTerm*12; //term of loan multiplied by 12 months equals total months of term
                    
                    double amLoanOne = Double.parseDouble(mortgageTextField1.getText());
                    //initiates the amount of loan balance after initial amount
                    double monPay = amLoanOne*monIntRate/(1-(Math.pow((1+monIntRate), (-totMons))));
                    mortgagecalculatedTextField4.setText(Math.round(monPay) + "");
                    //calculates the mortgage payment
                    double intsPaid = amLoanOne*monIntRate;
                    interestcalculatedTextField6.setText(Math.round(intsPaid) + "");
                    //calculates the interest paid
                    double princPaid = monPay - intsPaid;
                    //calculates the principal paid
                    double amLoanNxt = amLoanOne - princPaid;
                    loancalculatedTextField5.setText(Math.round(amLoanNxt) + "");
                    }
                    //calculates the amount of loan balance after initial amount 
                          // TODO add your handling code here:
      
        }                                          
    
        private void jMenuItem2ActionPerformed(java.awt.event.ActionEvent evt) {                                           
          double [] intRates = new double [3];
            for (double i = 0; i<intRates.length; i++)
            {
                double monIntRate = intRates[i]/12;
             //interest rate divided by 12 months equals the monthly interest rate
            
            double totMons = 15.*12; //term of loan multiplied by 12 months equals total months of term
            
            double amLoanOne = Double.parseDouble(mortgageTextField1.getText());
            //initiates the amount of loan balance after initial amount
            double monPay = amLoanOne*monIntRate/(1-(Math.pow((1+monIntRate), (-totMons))));
          mortgagecalculatedTextField4.setText(Math.round(monPay) + "");
            //calculates the mortgage payment
            double intsPaid = amLoanOne*monIntRate;
            interestcalculatedTextField6.setText(Math.round(intsPaid) + "");
            //calculates the interest paid
            double princPaid = monPay - intsPaid;
            //calculates the principal paid
            double amLoanNxt = amLoanOne - princPaid;
            loancalculatedTextField5.setText(Math.round(amLoanNxt) + "");
            //calculates the amount of loan balance after initial amount 
                    // TODO add your handling code here:  // TODO add your handling code here:
        }                                          
        }
        private void jMenuItem3ActionPerformed(java.awt.event.ActionEvent evt) {                                           
         double [] intRates = new double [3];
         for (int i = 0; i<intRates.length; i++)
         {
             double monIntRate = intRates [i]/12;
          //interest rate divided by 12 months equals the monthly interest rate
            
            double totMons = 30.*12; //term of loan multiplied by 12 months equals total months of term
            
            double amLoanOne = Double.parseDouble(mortgageTextField1.getText());
            //initiates the amount of loan balance after initial amount
            double monPay = amLoanOne*monIntRate/(1-(Math.pow((1+monIntRate), (-totMons))));
          mortgagecalculatedTextField4.setText(Math.round(monPay) + "");
            //calculates the mortgage payment
            double intsPaid = amLoanOne*monIntRate;
            interestcalculatedTextField6.setText(Math.round(intsPaid) + "");
            //calculates the interest paid
            double princPaid = monPay - intsPaid;
            //calculates the principal paid
            double amLoanNxt = amLoanOne - princPaid;
            loancalculatedTextField5.setText(Math.round(amLoanNxt) + "");
            //calculates the amount of loan balance after initial amount 
         }            // TODO add your handling code here:  // TODO add your handling code here:   // TODO add your handling code here:
        }                                          
    
        /**
         * @param args the command line arguments
         */
        public static void main(String args[]){
         {
            java.awt.EventQueue.invokeLater(new Runnable() {
    
                
                    @Override
                public void run() {
                    new MortgageGUI().setVisible(true);
                }
            });
         }
        // Variables declaration - do not modify                     
        private javax.swing.JButton calculateButton7;
        private javax.swing.JButton clearjButton8;
        private javax.swing.JTextField interestcalculatedTextField6;
        private javax.swing.JLabel interestrateLabel5;
        private javax.swing.JTextField interestrateTextField3;
        private javax.swing.JLabel jLabel11;
        private javax.swing.JLabel jLabel7;
        private javax.swing.JLabel jLabel9;
        private javax.swing.JMenu jMenu1;
        private javax.swing.JMenu jMenu2;
        private javax.swing.JMenuBar jMenuBar1;
        private javax.swing.JMenuItem jMenuItem1;
        private javax.swing.JMenuItem jMenuItem2;
        private javax.swing.JMenuItem jMenuItem3;
        private javax.swing.JPanel jPanel1;
        private javax.swing.JTextField loancalculatedTextField5;
        private javax.swing.JLabel loantermLabel3;
        private javax.swing.JTextField loantermTextField2;
        private javax.swing.JLabel mortgageLabel1;
        private javax.swing.JTextField mortgageTextField1;
        private javax.swing.JTextField mortgagecalculatedTextField4;
        // End of variables declaration                   
    }

  2. #22
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,513
    Rep Power
    25

    Default

    Is there a reason that you read the interest rates file in an action listener method instead of the in the initialization part of the program? Normally you would get all the data into the program before the user starts using it.
    The exception would be if the user was to chose what file to read.
    In your case, the name of the file is coded in the program.
    You should move the reading of the interest rate file to the initialization section of the program and not have it inside of an action listener.

    it does not get the values from reading the text file
    I still suggest that you work out how to do that in a small simple program without all the other junk that is not used for reading in the file and extracting the numbers.

Page 2 of 2 FirstFirst 12

Similar Threads

  1. Help with reading file into array
    By xkillswitchx14 in forum New To Java
    Replies: 2
    Last Post: 04-28-2011, 10:24 PM
  2. Reading csv file into 2D array - HELP!!!
    By mikeg in forum New To Java
    Replies: 17
    Last Post: 04-12-2011, 08:36 AM
  3. Help with reading from file into an array
    By Trad in forum New To Java
    Replies: 3
    Last Post: 10-22-2010, 12:16 PM
  4. Reading from a file to make an array
    By Bomber_Will in forum New To Java
    Replies: 11
    Last Post: 01-21-2009, 08:19 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
  •