Results 1 to 4 of 4
  1. #1
    rsmithjr316 is offline Member
    Join Date
    Jan 2011
    Posts
    9
    Rep Power
    0

    Default Moretgage Calculation

    Hello Everyone, I am new to the Java development world. I have a program that I am having a little issue with diplaying the proper mon htly payment. when First, when I was entering in a mortgage amount, selecting an interest rate and hitting calculate, I wasn't getting a response. Now when trying to figure out the math issues, I amde osme changes in the (index == 0) under the actionPerformed, I get the following error:

    cannot find symbol symbol : method parseString(java.lang.String) location: class java.lang.Double mortgageAmount = Double.parseString(amt);

    Here is a copy of the code. Any help would be greatly appreciated. Having a really hard time. Thanks.

    Java Code:
     import javax.swing.*;
     import java.awt.*;
     import java.awt.event.*;
     import java.text.NumberFormat;
     import java.lang.*;
    
     public class MortgageCalculate3 extends JFrame implements ActionListener
       {
    
    
       JButton endButton = new JButton("Exit");
       JButton Calc = new JButton("Calculate");
       JButton clrAll  = new JButton("Clear");
       JComboBox opt = new JComboBox();
       JLabel amntLabel = new JLabel("Loan Amount");
       JLabel optLabel = new JLabel();
       JTextArea text;
       JTextField amntField  = new JTextField(5);
       JPanel topPanel = new JPanel();
       JPanel bottomPanel = new JPanel();
       JPanel centerPanel = new JPanel();
    	  
         double mortgageAmount = 0.0;    //principal
         double termYears[] = {7.0, 15.0, 30.0};         //term
         double interestRate[] = {0.0535, 0.055, 0.0575}; //interest rate
         double monthlyPayments; //mortgage payment amount
         int lineCount = 0;
         int count = 0;
         int intCount = 4;
    
       public static void main(String[] args)
         {
          MortgageCalculate3 gui = new MortgageCalculate3();
          gui.go();
    
    
          }//end of main
    
          public void go()
            {
             JFrame frame1 = new JFrame("Mortgage Calculator");
             getContentPane().setLayout(
                new BoxLayout(getContentPane(),BoxLayout.Y_AXIS));
    
    
            //Larger scrolling window
             text = new JTextArea(20,50);
             text.setLineWrap(true);
             JScrollPane scroller = new JScrollPane(text);
             scroller.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
             scroller.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
             scroller.setVisible(true);
    
             //ActionListener section
    
            endButton.addActionListener(this);
            Calc.addActionListener(this);
            clrAll.addActionListener(this);
            amntField.addActionListener(this);
            opt.addActionListener(this);
    
    
    
             //Frame and Pane settings
             topPanel.add(amntLabel);
             topPanel.add(amntField);
             topPanel.add(opt);
                 opt.addItem("7 years at 5.35%");
                 opt.addItem("15 years at 5.5%");
                 opt.addItem("30 years at 5.75%");
                 opt.setEnabled(true);
             pack();
    
             centerPanel.add(text);
             centerPanel.add(scroller);
             scroller.setViewportView(text);
    
             bottomPanel.add(endButton);
             bottomPanel.add(clrAll);
             bottomPanel.add(Calc);
    
             frame1.getContentPane().add(BorderLayout.NORTH,topPanel);
             frame1.getContentPane().add(BorderLayout.CENTER,centerPanel);
             frame1.getContentPane().add(BorderLayout.SOUTH,bottomPanel);
    
    
             frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
             frame1.setSize(600,450);
             frame1.setLocation(100,100);
    
             frame1.setVisible(true);
    
    
    
             }//end of the go constructor
    
             public void actionPerformed(ActionEvent event)
               {
    
    
               Object source = event.getSource();
    
               if (source == endButton) {
                   System.exit(0);
    
               }//end of endButton
    
    
               if (source == Calc) {
    
                 int startIndex = opt.getSelectedIndex();
    
                   if(startIndex == 0){
    
    	               
    	           // Get the amount
          			String amt = amntField.getText();
         		  // Convert this to a number
         			mortgageAmount = Double.parseString(amt);
    
    	               
                   }//end of opt0
    
                   if(startIndex == 1){
                   }//end of opt1
    
                   if(startIndex == 2){
                   }//end of opt2
                   
                 mtgCalc();
               }//end of submit source
    
    
               if (source == clrAll) {
                   text.setText(null);
                   amntField.setText(null);
               }//end of clrAll if statement
         }//end of actionPerformed constructor
    
         public void mtgCalc()
                 {
                 monthlyPayments = ((interestRate[0] / 12) * mortgageAmount) /
            (1- (Math.pow (1+ (interestRate[0] / 12), (termYears[0] * -12))));
    
               text.append("Monthly Payment: ");
               text.append(String.valueOf(NumberFormat.getCurrencyInstance().format(monthlyPayments)));
               }//end of mtgCalc
    
         private static MortgageCalculate3 frame1;
         private static MortgageCalculate3 window;
       }//end of program
    Moderator Edit: Code tags added
    Last edited by Fubarable; 01-17-2011 at 04:24 AM. Reason: Moderator Edit: Code tags added

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by rsmithjr316 View Post
    ... I get the following error:

    cannot find symbol symbol : method parseString(java.lang.String) location: class java.lang.Double mortgageAmount = Double.parseString(amt);
    You need to believe what the error message is telling you, and it's saying that the Double class doesn't have a "parseString(String) method which is in fact correct. Rather than just give you the solution, I consider this a good exercise for you to learn to use the Java API, and I recommend that you go to the Java API, look up the Double class and find the correct method which is similar to what you're trying to call but a little different.

    Luck!

  3. #3
    rsmithjr316 is offline Member
    Join Date
    Jan 2011
    Posts
    9
    Rep Power
    0

    Default Thanks!

    Fubarable, thanks I got that figured out and it claculates the first selection which is index == 0. When I select the other two, I get the same number. Do I need to put the same code in each index? This is where I get a little lost in the math side.

    Thanks

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by rsmithjr316 View Post
    Fubarable, thanks I got that figured out and it claculates the first selection which is index == 0. When I select the other two, I get the same number. Do I need to put the same code in each index? This is where I get a little lost in the math side.

    Thanks
    Well, you've got to put some code in there, right? Here's your calculation code:

    Java Code:
          if (source == Calc) {
             int startIndex = opt.getSelectedIndex();
             if (startIndex == 0) {
                String amt = amntField.getText();
                mortgageAmount = Double.parseDouble(amt);
             }
             if (startIndex == 1) {
             }
             if (startIndex == 2) {
             }
             mtgCalc();
          }
    You only get the mortgageAmount if the first option is chosen in the JComboBox, why? Don't you need to get this data and update the mortgageAmount variable regardless of which JComboBox item is selected? The JComboBox selection should only change your interest rate and your loan duration but should have no effect on the mortgageAmount, so perhaps this data extraction should not be inside of your if block.

    Then in your mtgCalc method:
    Java Code:
       public void mtgCalc() {
          monthlyPayments = ((interestRate[0] / 12) * mortgageAmount)
                   / (1 - (Math.pow(1 + (interestRate[0] / 12), (termYears[0] * -12))));
          text.append("Monthly Payment: ");
          text.append(String.valueOf(NumberFormat.getCurrencyInstance().format(monthlyPayments)));
       }
    You are hard-coding the calculation to use the 0th item in the interestRate array and the 0th item in the termYears array, so that no matter which item is selected in the combobox, you're effectively ignoring it and blindly calculating as if the first item were always chosen. Perhaps you should use your startIndex value to choose the right value from the two arrays, interestRate and termYears.


    Also, when posting code here, please use code tags so that your code will retain its formatting and thus will be readable -- after all, your goal is to get as many people to read your post and understand your code as possible, right?

    To do this, highlight your pasted code (please be sure that it is already formatted when you paste it into the forum; the code tags don't magically format unformatted code) and then press the code button, and your code will have tags.

    Another way to do this is to manually place the tags into your code by placing the tag [code] above your pasted code and the tag [/code] below your pasted code like so:

    Java Code:
    [code]
      // your code goes here
      // notice how the top and bottom tags are different
    [/code]
    I've added code tags to your post above to help make it readable.
    Last edited by Fubarable; 01-17-2011 at 04:35 AM.

Similar Threads

  1. Jtextfield value get and calculation
    By globo in forum New To Java
    Replies: 11
    Last Post: 10-25-2010, 05:51 AM
  2. Calculation with char
    By chuckbalzer in forum New To Java
    Replies: 7
    Last Post: 09-20-2010, 06:29 PM
  3. Help with the calculation of a variable.
    By rarschach in forum New To Java
    Replies: 14
    Last Post: 01-24-2010, 03:50 PM
  4. Need help with doing a calculation in Java
    By John D. in forum New To Java
    Replies: 6
    Last Post: 02-25-2009, 12:44 AM
  5. Problem with Calculation ....
    By danny000 in forum New To Java
    Replies: 1
    Last Post: 04-15-2008, 03:42 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •