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

    Default Help with GUI, Array, and Reading File

    Hi, I am back again. I need help with a GUI and how to implement an array from a sequential file. I wrote the sequential file in a notepad text editor, intRates.txt. I want the values in the file to be read into the array. I want to use the values of the array to be inserted into places where I use intRate as a variable. I will post my code. Any suggestions are excellent. I appreciate it.

    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>                        
        
            
          public static void main (String[]args ) throw IOException
            {
                final int ROWS = 2;
            
            int Counter = 0;
            File f = new File("intRates.txt");
            if (!(f.exists()))
            {
                System.out.println("Program is closing");
            
            System.exit(0);
            
            }
            try {
                Scanner InputFile = new Scanner (f);
            } catch (FileNotFoundException ex) {
                Logger.getLogger(MortgageGUI.class.getName()).log(Level.SEVERE, null, ex);
            }
            int [] intRates = new int [ROWS];
            
            }  
            
            
        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) {                                           
            double [] intRates = new double[2];
            for (int i =0; i < 2; i++)
            {
            double monIntRate = .0535/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 monIntRate = .0550/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 monIntRate = .0575/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
         */
         {
            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. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Whoa, that's quite a lot of code. Your post mentions a few "I wants" but is short on specifics. Can you tell us as specifically as possible exactly where you're stuck? If you're stuck on a general thing such as reading information into a program from a file, then we'll likely refer you to a tutorial page and tell you to break big problems into little problems and try to solve each little problem one at a time. If on the other hand, you're stuck on a specific well-defined step, we can usually give you more precise and helpful help, and so again, the better and more precisely you can describe your problem, likely the better the experience you'll get here.

    Best of luck!

  3. #3
    bamagirl31 is offline Member
    Join Date
    May 2011
    Posts
    47
    Rep Power
    0

    Default Help with GUI, Array, and Reading File

    Here is what my application is not doing. It builds, but it does not show the GUI. It just shows "Program is closing". I think if I can figure where I am going wrong with that, the rest will come along. I fixed where my array accesses the elements. I need help with that part. Thanks.

  4. #4
    bamagirl31 is offline Member
    Join Date
    May 2011
    Posts
    47
    Rep Power
    0

    Default

    Or I believe I fixed the part where my array will access the elements, I should say. Thanks.

  5. #5
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,549
    Rep Power
    23

    Default

    So what do you need help with now?

  6. #6
    bamagirl31 is offline Member
    Join Date
    May 2011
    Posts
    47
    Rep Power
    0

    Default Still need help

    Hi, I still need help with being able to view my GUI. It reads "Program is closing" on the build. It does not show the GUI. Can you help? Thanks.

  7. #7
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,549
    Rep Power
    23

    Default

    To see where your program is executing, add some printlns to output some message when they are executed.
    Start with in the main() method and then add them to the first constructor called and then inside of each method that is called.
    The printed output should show you where the execution flow is going. It the printed output doesn't show enough, add more printlns.

  8. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Quote Originally Posted by bamagirl31 View Post
    Hi, I still need help with being able to view my GUI. It reads "Program is closing" on the build. It does not show the GUI. Can you help? Thanks.
    And also look through your code where you'll find a "decapitated" main method that someone (you?) removed the method header on. Murder most foul.

  9. #9
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    It reads "Program is closing"
    Good - because that norrows it down quite a bit. There seems to be only one place where that message is generated:

    Java Code:
    public static void main (String[]args ) throw IOException {
        final int ROWS = 2;
            
        int Counter = 0;
        File f = new File("intRates.txt");
        if(!(f.exists())) {
            [color=red]System.out.println("Program is closing");[/color]
            
            System.exit(0);
        }
        try {
            Scanner InputFile = new Scanner (f);
        } catch(FileNotFoundException ex) {
            Logger.getLogger(MortgageGUI.class.getName()).log(Level.SEVERE, null, ex);
        }
        int [] intRates = new int [ROWS];
    }
    (Your brace placement is inconsistenet, so I've formatted this according to my own tastes.) So we know that f.exists() is false. The next step would be to add a line that uses System.out.println() to say exactly what f is at this point: we know the name of the file, but it would be good to have the program print out the location where it is looking for the file. File has methods like getAbsolutePath() that might help.

  10. #10
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    When she fixes that, she'll stumble onto the crime scene. Yeah, I'm fixated on that, but it caused a neat bit of memory-blowing recursion for me when I tried her code out.

  11. #11
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    There's a non-static initializer block that was created by removing the signature from a previous main method. If you create a new object and setVisible(true); in the current main method, that new object will call the initializer block on creation which will create a new object, etc.

  12. #12
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,549
    Rep Power
    23

    Default

    Right. I found my problem. When I get a second version of an earlier post I add a suffix to the class name.
    I missed one spot so I think I was using the old version there.
    MortgageGUI_1 vs MortgageGUI

  13. #13
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    Quote Originally Posted by Fubarable View Post
    When she fixes that, she'll stumble onto the crime scene. Yeah, I'm fixated on that, but it caused a neat bit of memory-blowing recursion for me when I tried her code out.
    I agree.

    @OP: A "non-static initializer block" is a bunch of code within braces but not part of a constructor or method. If you haven't learnt about them then you almost certainly shouldn't be using them. In your case the block is near the end and involves the event queue. The place for that code probably inside your main() method.

    Just an aside, but NetBeans imposes a certain program structure. It allows you to change that, but it doesn't (and can't) magically give you the understanding that will allow you to do so. At some point you will have to learn what that invokeLater() business is all about and, on that basis, figure out where that statement should go.

  14. #14
    bamagirl31 is offline Member
    Join Date
    May 2011
    Posts
    47
    Rep Power
    0

    Default RE::

    Hi, I am still attemptint to get my GUI to show. The program shows it builds successfully. I do not know why it is not appearing when I run the file. Please help. Thanks. I did look at a tutorial or how to video, and I did place the file in the wrong place. It was not placed in the correct directory in Netbeans. I still need help getting the GUI to show when I run the program.
    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>                        
        
            
         
               
           
           public static void main (String []args)
           {
           
           }
        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 {
                final int ROWS = 2;
                    int Counter = 0;
                    File f = new File ("intRates.txt");
                    
                    if (!(f.exists()))
                    {
                        System.out.println("Program is closing");
                    
                    System.exit(0);
                    
                    }
                    
                        Scanner InputFile = new Scanner (f);
                        
                      
                    int [] intRates = new int [ROWS];
                    while(InputFile.hasNext())
                    {
                          intRates[Counter] = InputFile.nextInt(); 
                           
                     Counter++;
                    
                }
                    for (int i =0; i < 2; 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:
            } catch (FileNotFoundException ex) {
                Logger.getLogger(MortgageGUI.class.getName()).log(Level.SEVERE, null, ex);
            }
        }                                          
    
        private void jMenuItem2ActionPerformed(java.awt.event.ActionEvent evt) {                                           
          double [] intRates = new double [2];
            for (int i = 0; i<2; i++)
            {
                double monIntRate = intRates[1]/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 [2];
         for (int i = 0; i<2; i++)
         {
             double monIntRate = intRates [2]/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
         */
         {
            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                   
    }

  15. #15
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,549
    Rep Power
    23

    Default

    Did you try what I suggested in post #7?
    Add lots of printlns with different messages in different methods, execute the code and see where the execution goes by looking at what is printed out. Start with the main() method, then to the constructor, then in each method that is called.
    Find where you set the GUI visible and put one there also.
    Run the program and copy the printout here if you have questions.

  16. #16
    bamagirl31 is offline Member
    Join Date
    May 2011
    Posts
    47
    Rep Power
    0

    Default RE::

    Hi, I solved one problem and find I am not able to access array from my textfile. Can you all help me out with this one? The problem with not getting the GUI to show had to do with the last part of the code before variable declaration. There had to be a public static void included. I cannot read the text file into an array for my menu items. 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 {
                final int ROWS = 3;
                    int Counter = 0;
                    File f = new File ("intRates.txt");
                    
                    if (!(f.exists()))
                    {
                        System.out.println("Program is closing");
                    
                    System.exit(0);
                    
                    }
                    
                        Scanner InputFile = new Scanner (f);
                        
                      
                    int [] intRates = new int [ROWS];
                    while(InputFile.hasNext())
                    {
                          intRates[Counter] = InputFile.nextInt(); 
                           
                    
                    
                    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:
            } catch (FileNotFoundException ex) {
                Logger.getLogger(MortgageGUI.class.getName()).log(Level.SEVERE, null, ex);
            }
             
        }                                          
    
        private void jMenuItem2ActionPerformed(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 = 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                   
    }

  17. #17
    bamagirl31 is offline Member
    Join Date
    May 2011
    Posts
    47
    Rep Power
    0

    Default

    With jmenuItem1 ActionPerformed, I have code to try to read the file and put it into an array. I want to use one of the elements in a calculation. Thanks.

  18. #18
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,549
    Rep Power
    23

    Default

    I cannot read the text file into an array f
    I'd recommend you write a small simple program to work out the details of reading in a text file and putting the data into an array. Your posted code has too much useless junk in it that is not needed for the task of reading the file's data into an array. When you get the technique down, then you can copy the code into the big program.

  19. #19
    bamagirl31 is offline Member
    Join Date
    May 2011
    Posts
    47
    Rep Power
    0

    Default RE::

    Hi, thanks. I tried another way of read the text from a file in another program and got it to work. Now, I have a problem with the string I have from the text file and putting this into an array. My array has value of double. I am confused on this part. I will insert part of my code I have from the GUI application. Any suggestions are greatly appreciated. Thanks.
    Java Code:
    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;    
      while ((strLine = br.readLine()) != null)   {
       // Print the content on the console
       
           
            
    
    
                    String [] strLine = new String[3];
                    
                     monIntRate = strLine[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:
            } catch (FileNotFoundException ex) {
                Logger.getLogger(MortgageGUI.class.getName()).log(Level.SEVERE, null, ex);
            }
             
        }

  20. #20
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,549
    Rep Power
    23

    Default

    I have a problem with the string I have from the text file and putting this into an array
    Can you write a small program that shows your problem.
    Add some printlns to show what is read and some more printlns to show the values as you get them from the line that is read.

    The code that you posted doesn't do anything with what is read into the variable: strLine.
    Does what you posted compile?

Page 1 of 2 12 LastLast

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
  •