[x]
Posted via EE Mobile

Search, ask, and monitor your questions on the go with EE Mobile. Visit Experts Exchange from your mobile device and never be out of touch again.

09/15/2008 at 08:06AM PDT, ID: 23732207
[x]
Attachment Details
[x]
The Solution Rating System

With so many solutions, how can you tell which solutions are most likely to help you and which ones are not? To provide you with a tool to use, we rate our solutions based on various elements that most accurately determine if a solution is a quality solution. To explain what factors affect the solution rating, here are the elements we take into consideration when formulating our solution rating.

  • The Grade of the Solution
  • The Zone Rank of the Expert Providing the Solution
  • The Number of Author and Expert Comments
  • The Number of Experts Contributing
  • The Feedback of the Community

Your Input Matters
Because of the way the system is set up, the most important variable in this equation is you. As a member of Experts Exchange, you are able to cast your vote on the quality of the solutions in regard to how complete, accurate, helpful and easy to understand each solution is. When you provide your feedback, each rating is adjusted accordingly. So, if you see a solution that has a poor rating that you think is a good solution, let us know by rating it. As you do, the rating will be adjusted and will become more accurate for other members of our site.

If you have any suggestions that you would like to make for our rating system, please ask a question in the Suggestions Zone of Community Support.

Thank you!

7.8

***Exception Handling and functioning of JButtons***

Asked by antnupe0320 in New to Java Programming, Java Programming Language

Tags: JAVA

Cannot seem to get my exception Handling to work correctly. I have tried multiple times to work this issue in other forms and have made corrections but am still unable to get the application to work correctly. My first exception does not work properly becasue it posts an error message that a button needs to be selected but then proceeds as desired. When I use the buttons instead of the manual entering then it does not calculate and gives error message that all fields need to be completed-if I use the buttons after manual entering then it just jumps to the amortize instead of calculating first.
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
111:
112:
113:
114:
115:
116:
117:
118:
119:
120:
121:
122:
123:
124:
125:
126:
127:
128:
129:
130:
131:
132:
133:
134:
135:
136:
137:
138:
139:
140:
141:
142:
143:
144:
145:
146:
147:
148:
149:
150:
151:
152:
153:
154:
155:
156:
157:
158:
159:
160:
161:
162:
163:
164:
165:
166:
167:
168:
169:
170:
171:
172:
173:
174:
175:
176:
177:
178:
179:
180:
181:
182:
183:
184:
185:
186:
187:
188:
189:
190:
191:
192:
193:
194:
195:
196:
197:
198:
199:
200:
201:
202:
203:
204:
205:
206:
207:
208:
209:
210:
211:
212:
213:
214:
215:
216:
217:
218:
219:
220:
221:
222:
223:
224:
225:
226:
227:
228:
229:
230:
231:
232:
233:
234:
235:
236:
237:
238:
239:
240:
241:
242:
243:
244:
245:
246:
247:
248:
249:
250:
251:
252:
253:
254:
255:
256:
257:
258:
259:
260:
261:
262:
263:
264:
265:
266:
267:
268:
269:
270:
271:
272:
273:
274:
275:
276:
277:
278:
279:
280:
281:
282:
283:
284:
285:
286:
287:
288:
289:
290:
291:
292:
293:
294:
295:
296:
297:
298:
299:
300:
301:
302:
303:
304:
305:
306:
307:
308:
309:
310:
311:
312:
313:
314:
315:
316:
317:
318:
319:
320:
321:
322:
323:
324:
325:
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import javax.swing.text.*;
import javax.swing.border.Border;
import javax.swing.border.EmptyBorder;
import java.text.*;
import javax.swing.JTextField.*;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.Locale;
   
public class MortPay extends JFrame implements ActionListener
{//start of program
    double rate = 0;  //Sets interest rate variable    
    double payment = 0; //Sets variable for monthly payment
    double loan = 0; //Sets Variable for Loan Amount
    int term = 0; //Sets terms of loan in years    
    double interest = 0; //Sets variable for monthly interest
    int notePeriod = 0; //Sets variable for Loan length in months
 
    String mTerm[] = {"7", "15", "30"};// string for mortage Lengths
    String mRate[] = {"5.35%", "5.50%", "5.75%"};// string for interest rates
 
    //Labels and Input Fields
    JPanel row1 = new JPanel(new GridLayout(1, 2));//creates new panel
    JLabel loanLabel = new JLabel("Mortgage Amount: $",JLabel.LEFT);//labels panel and location
    JTextField loanTxt = new JTextField(8);//sets definition of feild
    
    //creates new panel, row and sets location
    JPanel row2 = new JPanel();
    JLabel wholeamt = new JLabel ("(Enter amount in whole dollars without commas or decimals)", JLabel.LEFT);
    
    //creates new panel, row and sets location
    JPanel row3 = new JPanel (new GridLayout(1, 1));
    JLabel termLabel = new JLabel("Length of Loan(in years):",JLabel.LEFT);
    JTextField t = new JTextField(8);
    
    //creates new panel, row and sets location
    JPanel row4 = new JPanel (new GridLayout(1, 1));//creates new panel
    JLabel rateLabel = new JLabel("Interest Rate:", JLabel.LEFT);
    JTextField r = new JTextField(5);
    
    //creates new panel, row and sets location
    JPanel row5 = new JPanel (new GridLayout(1, 1));//creates new panel
    JLabel dec = new JLabel ("(Enter interest rate as a decimal number without % sign)", JLabel.LEFT);
    
    //creates new panel for radio buttons
    JPanel radioPanel = new JPanel();//creates new radio panel
    JRadioButton buttonA = new JRadioButton("7 Years at 5.35%" , false);//defines display of button
    JRadioButton buttonB = new JRadioButton("15 Years at 5.50%" , false);//defines display of button
    JRadioButton buttonC = new JRadioButton("30 Years at 5.75%", false);//defines display of button
 
    JPanel row6 = new JPanel(new GridLayout(1, 1));//creates new panel
    JLabel paymentLabel = new JLabel("Your Monthly mortgage payment will be:", JLabel.LEFT);
    JTextField paymentTxt = new JTextField(8);
 
    //Create buttons
    JPanel button = new JPanel(new FlowLayout(FlowLayout.CENTER, 8, 8));//creates new panel and location
    JButton clearButton = new JButton("Clear");//creates button "clear"
    JButton calculateButton = new JButton("Calculate");//creates button "calculate"
    JButton amortizeButton = new JButton("Amortize Payments"); //creates button "amortize"   
    JButton quitButton = new JButton("End or Quit");//creates button "end or quit"
    
 
    JTextArea displayArea = new JTextArea(10, 30);//creates display area for amortization
    JScrollPane scroll = new JScrollPane(displayArea);//allows pane to be scrolled
 
public MortPay ()
{
        super ("McBride Financial Services Mortgage Payment Calculator "); //Header
        setSize(550,650); //GUI frame size
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//to make sure that program stops after quting
        setResizable(false);//disables window resizing
        Container pane = getContentPane();
        pane.setLayout(new BoxLayout( pane, BoxLayout.Y_AXIS));//sets layout
 
        Border rowborder = new EmptyBorder( 3, 10, 3, 10 ); //Creates a border around elements within the frame
 
        //GUI layout of panes and rows
        JPanel panel = new JPanel(new GridLayout(9,1));//sets panel and grid layout
        
        panel.add(row1);//adds first row
        row1.add(loanLabel);//creates label for row
        row1.add(loanTxt);//creates text reading for row
        row1.setLayout (new FlowLayout(FlowLayout.LEFT, 1,1));//sets row of panel and location
        row1.setMaximumSize( new Dimension( 200, row1.getMinimumSize().height));//sets property for row
        row1.setBorder(rowborder);// sets border for row
        
        panel.add (row2);//adds second row
        row2.add (wholeamt); //creates label for row
        row2.setLayout (new FlowLayout(FlowLayout.LEFT));//sets layout and location
        
        panel.add(row3);//adds third row
        row3.add(termLabel);//creates label for row
        row3.add(t);//creates text reading for row and denotes variable of t
        row3.setLayout (new FlowLayout(FlowLayout.LEFT, 1,1));//sets layout and location
        row3.setMaximumSize( new Dimension( 200, row1.getMinimumSize().height));//sets property for row
        row3.setBorder(rowborder);// sets border for row
        
        panel.add(row4);//adds third row
        row4.add(rateLabel);//creates label for row
        row4.add(r);//creates text reading for row and denotes variable of r
        row4.setLayout (new FlowLayout(FlowLayout.LEFT, 1,1));//sets layout and location
        row4.setMaximumSize( new Dimension( 200, row4.getMinimumSize().height));//sets property for row
        row4.setBorder(rowborder);// sets border for row
        
        panel.add (row5);//adds second row
        row5.add (dec); //creates label for row
        row5.setLayout (new FlowLayout(FlowLayout.LEFT));//sets layout and location
        
 
        panel.add(radioPanel);//creates radio panel for row
        ButtonGroup bgroup = new ButtonGroup();//creates button group
        bgroup.add(buttonA);//creates button "A" for group
        bgroup.add(buttonB);//creates button "B" for group
        bgroup.add(buttonC);//creates button "C" for group
 
        radioPanel.setLayout(new FlowLayout(FlowLayout.LEFT, 4, 4 ));//sets layout of radio panel and location
        radioPanel.add(buttonA);//creates panel for button a        
        radioPanel.add(buttonB);//creates panel for button b 
        radioPanel.add(buttonC);//creates panel for button c 
 
        //Creates panel for Radio Panel
        radioPanel.setMaximumSize( new Dimension( 200, radioPanel.getMinimumSize().height));
        //sets size for radio panel
        radioPanel.setBorder(rowborder);//sets border for radio panel (Row)
 
        panel.add(row6);//adds sixth row
        row6.add(paymentLabel);//creates label for row
        row6.add(paymentTxt);//sets text field for the row
        paymentTxt.setEnabled(false); //Disables payment amount editing
        row6.setLayout (new FlowLayout(FlowLayout.LEFT, 1,1));//sets layout and location
        row6.setMaximumSize( new Dimension( 200, row6.getMinimumSize().height));//sets property for row 
        row6.setBorder(rowborder);//sets border for row
 
        //Creates buttons
        panel.add(button); //Add button
        button.add(clearButton);//Add button
        button.add(calculateButton);//Add button
        button.add(amortizeButton);//Add button
        button.add(quitButton);//Add button
        
        pane.add(panel);
        button.setMaximumSize( new Dimension( 200, button.getMinimumSize().height));//sets property for row
 
       scroll.setBorder(BorderFactory.createEmptyBorder(10,10,10,10));//sets property for scroll area
        pane.add(scroll);// creates scroll 
 
        setVisible(true);//sets scroll panel to visible
        setContentPane(pane);//creates pane to contain amortize contents
 
        //Creating Action Listeners for buttons
        buttonA.addActionListener(this);//Add listener for  radio button a
        buttonB.addActionListener(this);//Add listener for  radio button b
        buttonC.addActionListener(this);//Add listener for  radio button c
        clearButton.addActionListener(this);//Add listener for clear button
        calculateButton.addActionListener(this);//Add listener for calculate button 
        amortizeButton.addActionListener(this); //Add listener for amortize button        
        quitButton.addActionListener(this);//Add listener for quit button
        
 
 
}
       //method to get input from buttons that user presses to determine action to be taken 
public void actionPerformed(ActionEvent event) 
{
            Object command = event.getSource();               
                {
            if (command == clearButton) 
                {            
               loanTxt.setText(null);//resets loan amount field
               paymentTxt.setText(null);//resets payment field
               displayArea.setText(null); //resets amortiztion area              
               t.setText("");//resets term field
               r.setText("");//resets rate field
               return;
                }  
            if (command == calculateButton) 
                {
               try
                {
               loan = Double.parseDouble(loanTxt.getText());
               rate = Double.parseDouble(r.getText());//retrieves input from user
               term = Integer.parseInt(t.getText());//retrieves input from user 
               }
     catch (NumberFormatException numberFormatException) //checks for valid number entries
               {
       	       JOptionPane.showMessageDialog(null, "ERROR, Please Complete all fields", "ERROR", JOptionPane.ERROR_MESSAGE);
               //pane for error message in the event of invalid entry//message display for imcomplete or incorrect entries
            return;
               }
    	
                        //Select rate and term
              if (buttonA.isSelected() == true) 
                {
               rate = 5.35;
               term = 7;
                }
            else if (buttonB.isSelected() == true) 
                {
               rate = 5.5;
               term = 15;
                }
            else if (buttonC.isSelected() == true) 
                {
               rate = 5.75;
               term = 30;
                }
            else //none of the buttons  are selected,  this is an actual error.  Throw an exception
                {
               JOptionPane.showMessageDialog(null, "ERROR, Please Select Years/Rate", "ERROR", JOptionPane.ERROR_MESSAGE);
               //pane for error message in the event of invalid entry
             
                }
 
               //Calculate monthly interest rate
               interest = ((rate / 100) / 12);//calculates monthly interest
 
               //Calculate total number of payments
               notePeriod = term * 12; // (for example, 5 years means notePeriod = 60)
 
               //Calculate monthly payment
            if (rate == 0.0) 
                {
               payment = loan / notePeriod;
                }
            else 
                {
               payment = (loan * interest) / (1 - Math.pow(1 + interest, -notePeriod));
               
                }
               
                 //Currency formatter
               NumberFormat myCurrencyFormatter;
               myCurrencyFormatter = NumberFormat.getCurrencyInstance(Locale.US);
               paymentTxt.setText(myCurrencyFormatter.format(payment));
               return;
                }
                
                } 
            
            if(command == amortizeButton) 
                {
                
            }
               double num = 0;
               double interest = 0;
               double loanbal = 0;                         
               double monthlyInterest = 0;
               String titles = "Month \t Mortgage \t Interest \t Loan Balance\n";
 
               monthlyInterest = (rate / 100) /12;
               loanbal = loan;
               displayArea.setText(titles);
 
               
              
 
            for(int counter = 1; counter <= notePeriod ; counter++)//loop to count down months of payment 
                {
               interest = loanbal * monthlyInterest;//variable to calculate interest paid each month
               
            if(interest <= 0)//condition to continue with for loop as long as interest is not "0"
               interest = 0;//intialize of variable
               num = payment - interest; // Interest amount paid 
               loanbal = loanbal - num; // Loan Principal balance paid
 
               //Format Variables
               DecimalFormat df = new DecimalFormat("\u00A4#,##0.00"); //Crrency
               DecimalFormat pf = new DecimalFormat("#,##0.00%"); //Percentages
               DecimalFormat mi = new DecimalFormat("#,##0.000%"); //Percentages
 
               //Puts Cursor at Begining of Text Area               
               displayArea.setCaretPosition(0);
               displayArea.append((counter) +  "\t" + df.format(payment) + "\t" + df.format(interest) + "\t" + df.format(loanbal) + "\n");
                     
            }
              
            
        
 
            if (command == quitButton)//button to end program 
                {
               System.exit(0);//command to exit program
                }
}
 
public void Payment()
{
               //Get user input
               loan = Double.parseDouble(loanTxt.getText());
 
               //Calculate monthly interest rate
               interest = ((rate / 100) / 12);
 
               //Calculate total number of payments
               notePeriod = term * 12;
 
               //Calculate monthly payment
            if (rate == 0.0)
                {
               payment = loan / notePeriod;
                }
            else
                {
               payment = (loan * interest) / (1 - Math.pow(1 + interest, -notePeriod));
                }
 
               //Currency formatter
               NumberFormat myCurrencyFormatter;//sets number format as currency format
               myCurrencyFormatter = NumberFormat.getCurrencyInstance(Locale.US);//set currency format as US
               paymentTxt.setText(myCurrencyFormatter.format(payment));//sets format of US to be applied to payment
}
public static void main (String[] arguments) //Main Method
{
               MortPay dap = new MortPay();
               dap.setVisible(true);//set to visible
               dap.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//stops running of program
            
}//End of Main
}//End of program
[+][-]09/15/08 08:10 AM, ID: 22479384

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]09/15/08 08:39 AM, ID: 22479746

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]09/15/08 03:12 PM, ID: 22483405

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]09/15/08 03:15 PM, ID: 22483417

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]09/15/08 08:52 PM, ID: 22484713

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]09/15/08 09:02 PM, ID: 22484759

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]09/15/08 09:05 PM, ID: 22484774

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]09/15/08 09:13 PM, ID: 22484808

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]09/15/08 10:37 PM, ID: 22485183

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]09/15/08 10:41 PM, ID: 22485195

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]09/15/08 10:44 PM, ID: 22485208

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]09/15/08 10:57 PM, ID: 22485239

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]09/15/08 11:04 PM, ID: 22485257

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]09/15/08 11:05 PM, ID: 22485261

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]09/15/08 11:08 PM, ID: 22485271

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]09/15/08 11:09 PM, ID: 22485277

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]09/15/08 11:11 PM, ID: 22485287

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]09/15/08 11:13 PM, ID: 22485291

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]09/15/08 11:25 PM, ID: 22485325

View this solution now by starting your 30-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

 

About this solution

Zones: New to Java Programming, Java Programming Language
Tags: JAVA
Sign Up Now!
Solution Provided By: mwvisa1
Participating Experts: 2
Solution Grade: A
 
 
[+][-]09/15/08 11:27 PM, ID: 22485329

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]09/15/08 11:33 PM, ID: 22485348

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]09/15/08 11:39 PM, ID: 22485393

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]09/15/08 11:40 PM, ID: 22485405

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]09/15/08 11:56 PM, ID: 22485525

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]09/16/08 12:04 AM, ID: 22485588

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]09/16/08 01:50 AM, ID: 22486183

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
 
Loading Advertisement...
20090824-EE-VQP-74 / EE_QW_2_20070628