Chat LIVE With Programming Experts! There Are 23 Online Right Now...

Welcome to Dream.In.Code
Become a Java Expert!

Join 244,306 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 781 people online right now. Registration is fast and FREE... Join Now!




Java GUI Assistance

 
Reply to this topicStart new topic

Java GUI Assistance

kooz
20 Dec, 2008 - 01:45 PM
Post #1

New D.I.C Head
*

Joined: 22 Nov, 2008
Posts: 6

I have a program that has a user enter the principal, interest rate and term of the rate and is to show the payment amount after it has entered all three and I get nothing but a little square when the output box comes up. Not sure where I went wrong. Any assistance is greatly appreciated.

CODE

Write the program in Java with a GUI and have it calculate and display the mortgage payment amount from user input of the amount of the mortgage, the term of the mortgage, and the interest rate
                    of the mortgage.  Allow the user to loop back and enter new data or quit.          
*/

import javax.swing.JOptionPane;
import java.text.DecimalFormat;

public class MortgageCalculator
{
    public static void main(String[] args)
    {
        //declare variables
        double principal, interest, answer, monthlyInterest;
        int term, numbPayments;

            
        //calling methods
        principal = getPrincipal();
        interest = getInterest()/100;
        term = getTerm();
        monthlyInterest = interest/12;
        numbPayments = term*12;
        answer = (principal*(monthlyInterest*Math.pow((1+monthlyInterest),numbPayments)))/(Math.pow((1+monthlyInterest),numbPayments) -1);
        output();
        finish();

    }
    
    //The getPrincipal() method will ask the user to input the principal amount
    public static double getPrincipal()
    {
        //declare method variable
        double principal = 0.0;
        
        String answer = JOptionPane.showInputDialog(null, "Enter the Mortgage amount\n(do not use commas or dollar signs)\n or click Cancel to exit:");
        
        return principal;
    }
    
    //The getInterest() method will retrieve the interest amount
    public static double getInterest()
    {
        //declare method variable
        double interest = 0.0;
        boolean done = false;
        
        //loop while not done
        while (!done)
        {
            try
                {
                    String rate = JOptionPane.showInputDialog(null, "Enter the interest rate\n(do not use the percentage sign)");
        
                    interest = Double.parseDouble(rate);
                    
                    //test for interest rate amount
                    if (interest<0.0) throw new NumberFormatException();
                    else done = true;
                }
                catch(NumberFormatException e)
                {
                    JOptionPane.showMessageDialog(null, "Please enter an interest rate.", "Error",JOptionPane.INFORMATION_MESSAGE);
                }
                }        
        return interest;
    }
    

    //the getTerm() method will get the term length of the loan
    public static int getTerm()
    
    {
        //declare method variable
        int term = 0;
        
        String number = JOptionPane.showInputDialog(null, "Enter the length of mortgage term, use numbers only:");
        
        return term;
    }
    
    //the output() method will display the monthly payment for the loan
    public static void output()
    {
        DecimalFormat twoDigits = new DecimalFormat("$#,000.00");
        
        JOptionPane.showMessageDialog(null,"Your Monthly Payment is:" + twoDigits.format(answer));
                
    
    }
    
    //the finish() method ends the program
    public static void finish()
    {
        System.exit(0);
    }
}


User is offlineProfile CardPM
+Quote Post


Martyr2
RE: Java GUI Assistance
20 Dec, 2008 - 02:56 PM
Post #2

Programming Theoretician
Group Icon

Joined: 18 Apr, 2007
Posts: 6,656



Thanked: 613 times
Expert In: C/C++, Java, VB, VB.NET, C#, PHP, Web Development, HTML & CSS, Javascript

My Contributions
Well the reason it was showing the box was because "answer" was coming out as the value NaN which means "not a number". And that was because you were attempting to get access to "answer" when answer was defined to the local main function only.

So by moving answer to a higher scope other functions can see it and use it.

Then the second problem you had was that in your getPrincipal and getTerm functions you never set the principal or the term variables to what the user entered. So whatever the user entered wasn't being put into the calculation.

As soon as you make these changes, all was left was to modify your format pattern a little so that it would show numbers correctly and you were on easy street.

java



import javax.swing.JOptionPane;
import java.text.DecimalFormat;

public class MortgageCalculator2
{
// Move answer to a higher scope so that many functions can see and manipulate it
private static double answer;

public static void main(String[] args)
{
//declare variables
double principal, interest, monthlyInterest;
int term, numbPayments;


//calling methods
principal = getPrincipal();
interest = getInterest()/100;
term = getTerm();
monthlyInterest = interest/12;
numbPayments = term*12;
answer = (principal*(monthlyInterest*Math.pow((1+monthlyInterest),numbPayments)))/(Math.pow((1+monthlyInterest),numbPayments) -1);
output();
finish();

}

//The getPrincipal() method will ask the user to input the principal amount
public static double getPrincipal()
{
//declare method variable
double principal = 0.0;

// Don't forget to set principal to what was entered
String principle = JOptionPane.showInputDialog(null, "Enter the Mortgage amount\n(do not use commas or dollar signs)\n or click Cancel to exit:");
principal = Double.parseDouble(principle);

return principal;
}

//The getInterest() method will retrieve the interest amount
public static double getInterest()
{
//declare method variable
double interest = 0.0;
boolean done = false;

//loop while not done
while (!done)
{
try
{
String rate = JOptionPane.showInputDialog(null, "Enter the interest rate\n(do not use the percentage sign)");

interest = Double.parseDouble(rate);

//test for interest rate amount
if (interest<0.0) throw new NumberFormatException();
else done = true;
}
catch(NumberFormatException e)
{
JOptionPane.showMessageDialog(null, "Please enter an interest rate.", "Error",JOptionPane.INFORMATION_MESSAGE);
}
}
return interest;
}


//the getTerm() method will get the term length of the loan
public static int getTerm()

{
//declare method variable
int term = 0;

// Again, make sure you set term to the value entered
String number = JOptionPane.showInputDialog(null, "Enter the length of mortgage term, use numbers only:");
term = Integer.parseInt(number);

return term;
}

//the output() method will display the monthly payment for the loan
public static void output()
{
// Change the pattern here to only show numbers and never leading zeros.
DecimalFormat twoDigits = new DecimalFormat("$#,##0.00");
JOptionPane.showMessageDialog(null,"Your Monthly Payment is:" + twoDigits.format(answer));


}

//the finish() method ends the program
public static void finish()
{
System.exit(0);
}
}


You will see the changes I made with in-code comments explaining what we did there. I simply moved answer to a higher scope so that your output function could see it and print it and made a few changes to the way you were setting values in your principal and term functions.

Enjoy!

"At DIC we be principal and term fixing code ninjas... our terms are no holds barred and our favorite person to beat up is the principal." decap.gif

This post has been edited by Martyr2: 20 Dec, 2008 - 02:57 PM
User is offlineProfile CardPM
+Quote Post

kooz
RE: Java GUI Assistance
20 Dec, 2008 - 03:03 PM
Post #3

New D.I.C Head
*

Joined: 22 Nov, 2008
Posts: 6


This makes so much sense now! Thank you soo much for this, I was staring at it for hours and couldn't figure it out!

Kudos to you!!


QUOTE(Martyr2 @ 20 Dec, 2008 - 02:56 PM) *

Well the reason it was showing the box was because "answer" was coming out as the value NaN which means "not a number". And that was because you were attempting to get access to "answer" when answer was defined to the local main function only.

So by moving answer to a higher scope other functions can see it and use it.

Then the second problem you had was that in your getPrincipal and getTerm functions you never set the principal or the term variables to what the user entered. So whatever the user entered wasn't being put into the calculation.

As soon as you make these changes, all was left was to modify your format pattern a little so that it would show numbers correctly and you were on easy street.

java



import javax.swing.JOptionPane;
import java.text.DecimalFormat;

public class MortgageCalculator2
{
// Move answer to a higher scope so that many functions can see and manipulate it
private static double answer;

public static void main(String[] args)
{
//declare variables
double principal, interest, monthlyInterest;
int term, numbPayments;


//calling methods
principal = getPrincipal();
interest = getInterest()/100;
term = getTerm();
monthlyInterest = interest/12;
numbPayments = term*12;
answer = (principal*(monthlyInterest*Math.pow((1+monthlyInterest),numbPayments)))/(Math.pow((1+monthlyInterest),numbPayments) -1);
output();
finish();

}

//The getPrincipal() method will ask the user to input the principal amount
public static double getPrincipal()
{
//declare method variable
double principal = 0.0;

// Don't forget to set principal to what was entered
String principle = JOptionPane.showInputDialog(null, "Enter the Mortgage amount\n(do not use commas or dollar signs)\n or click Cancel to exit:");
principal = Double.parseDouble(principle);

return principal;
}

//The getInterest() method will retrieve the interest amount
public static double getInterest()
{
//declare method variable
double interest = 0.0;
boolean done = false;

//loop while not done
while (!done)
{
try
{
String rate = JOptionPane.showInputDialog(null, "Enter the interest rate\n(do not use the percentage sign)");

interest = Double.parseDouble(rate);

//test for interest rate amount
if (interest<0.0) throw new NumberFormatException();
else done = true;
}
catch(NumberFormatException e)
{
JOptionPane.showMessageDialog(null, "Please enter an interest rate.", "Error",JOptionPane.INFORMATION_MESSAGE);
}
}
return interest;
}


//the getTerm() method will get the term length of the loan
public static int getTerm()

{
//declare method variable
int term = 0;

// Again, make sure you set term to the value entered
String number = JOptionPane.showInputDialog(null, "Enter the length of mortgage term, use numbers only:");
term = Integer.parseInt(number);

return term;
}

//the output() method will display the monthly payment for the loan
public static void output()
{
// Change the pattern here to only show numbers and never leading zeros.
DecimalFormat twoDigits = new DecimalFormat("$#,##0.00");
JOptionPane.showMessageDialog(null,"Your Monthly Payment is:" + twoDigits.format(answer));


}

//the finish() method ends the program
public static void finish()
{
System.exit(0);
}
}


You will see the changes I made with in-code comments explaining what we did there. I simply moved answer to a higher scope so that your output function could see it and print it and made a few changes to the way you were setting values in your principal and term functions.

Enjoy!

"At DIC we be principal and term fixing code ninjas... our terms are no holds barred and our favorite person to beat up is the principal." decap.gif


User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic

Time is now: 7/4/09 06:36PM

Live Java Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month