Results 1 to 4 of 4
Thread: Moretgage Calculation
- 01-17-2011, 02:40 AM #1
Member
- Join Date
- Jan 2011
- Posts
- 9
- Rep Power
- 0
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.
Moderator Edit: Code tags addedJava 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 programLast edited by Fubarable; 01-17-2011 at 03:24 AM. Reason: Moderator Edit: Code tags added
-
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!
- 01-17-2011, 03:21 AM #3
Member
- Join Date
- Jan 2011
- Posts
- 9
- Rep Power
- 0
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
-
Well, you've got to put some code in there, right? Here's your calculation code:
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.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(); }
Then in your mtgCalc method:
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.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))); }
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:
I've added code tags to your post above to help make it readable.Java Code:[code] // your code goes here // notice how the top and bottom tags are different [/code]
Last edited by Fubarable; 01-17-2011 at 03:35 AM.
Similar Threads
-
Jtextfield value get and calculation
By globo in forum New To JavaReplies: 11Last Post: 10-25-2010, 04:51 AM -
Calculation with char
By chuckbalzer in forum New To JavaReplies: 7Last Post: 09-20-2010, 05:29 PM -
Help with the calculation of a variable.
By rarschach in forum New To JavaReplies: 14Last Post: 01-24-2010, 02:50 PM -
Need help with doing a calculation in Java
By John D. in forum New To JavaReplies: 6Last Post: 02-24-2009, 11:44 PM -
Problem with Calculation ....
By danny000 in forum New To JavaReplies: 1Last Post: 04-15-2008, 02:42 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks