• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Tim Cooke
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Jeanne Boyarsky
Saloon Keepers:
  • Stephan van Hulst
  • Carey Brown
  • Tim Holloway
  • Piet Souris
Bartenders:

static method data validation in a dialog box app

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This program uses 3 dialog boxes to gather data to use in a future value calculation. My progam gets to the first dialog box but then exits. Here is the code:

import javax.swing.*;
import java.text.*;
/**
*
* @author
*/
public class FutureValueApp {

/** Creates a new instance of FutureValueApp */
public FutureValueApp() {
}

public static void main(String[] args){
String choice = "";
try{while (!(choice.equalsIgnoreCase("x"))){

String paymentString = JOptionPane.showInputDialog(
null, "Enter monthly payment: ", "Future Value", JOptionPane.PLAIN_MESSAGE);
double monthlyPayment = parseMonthlyPay(paymentString);

String rateString = JOptionPane.showInputDialog(
null, "Enter yearly interest rate: ", "Future Value", JOptionPane.PLAIN_MESSAGE);
double interestRate = Double.parseDouble(rateString);
double monthlyInterestRate = interestRate/12/100;

String yearsString = JOptionPane.showInputDialog(
null, "Enter number of years: ", "Future Value", JOptionPane.PLAIN_MESSAGE);
int years = Integer.parseInt(yearsString);
int months = years * 12;

double futureValue = calculateFutureValue(monthlyPayment,
months, monthlyInterestRate);

NumberFormat currency = NumberFormat.getCurrencyInstance();
NumberFormat percent = NumberFormat.getPercentInstance();
percent.setMinimumFractionDigits(2);
String message =
"Monthly payment: " + currency.format(monthlyPayment) + "\n"
+ "Yearly interest rate: " + percent.format(interestRate/100) + "\n"
+ "Number of years: " + years + "\n"
+ "Future value: " + currency.format(futureValue) + "\n\n"
+ "To continue, press Enter.\n"
+ "To exit, enter 'x': ";
choice = JOptionPane.showInputDialog(null,
message, "Future Value", JOptionPane.PLAIN_MESSAGE);
}//end of try
}
catch(NullPointerException e) {// catch that handles exception of user pressing the cancel button
System.exit(0);

}
//System.exit(0);
}//end of main method

private static double parseMonthlyPay(String payString){
double monthlyPayment = 0;
boolean tryAgain = true;
while(tryAgain) {
try{
monthlyPayment = Double.parseDouble(payString);
while (monthlyPayment <=0){
payString = JOptionPane.showInputDialog(
"Invalid monthly payment. \n"
+ "Please enter a positive number: ");
monthlyPayment = Double.parseDouble(payString);
}

}

catch(NumberFormatException e){
payString = JOptionPane.showInputDialog(
"Invalid monthly payment. \n"
+ "Please enter a number");
}
}
return monthlyPayment;
}

private static double calculateFutureValue(double monthlyPayment,
int months, double interestRate){
int i = 1;
double futureValue = 0;
while (i <= months) {
futureValue = (futureValue + monthlyPayment) *
(1 + interestRate);
i++;
}
return futureValue;
}


}
 
Teddy Salad
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
More on this app. Two static methods are used. parseMonthlyPay validates the first entry. It makes sure the monthly payment is a number and is positive. I want to eventually validate all inputs but I am doing this incrementally to make sure this is working first. The method calculateFutureValue takes the inputs monthly pay, interest rate, and years and comes up with the future value. Thanks.
 
author and iconoclast
Posts: 24204
44
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you've got a lot of this in your code:



"e" is a message that Java sends you to tell you something's gone wrong. Your reaction is to discard it without even looking at it -- that's bad, right? Especially given that your program is exiting prematurely, it's exceedingly likely that this catch block, or another one like it, is swallowing up Java's explanation of something that's gone wrong and hiding it from you.

Never discard exceptions. Always log or display them in some way. In this particular case, just add an e.printStackTrace(), and I'll bet you'll immediately be pointed to the problem line of code.
 
Teddy Salad
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It is a lot of code. I am trying to do an exercise in a book. I guess the logic is solve the problem with long inefficient code and then when you master the skills solve things with code that is sleeker. Where would you declare the trace?
 
Ranch Hand
Posts: 1078
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Ernest Friedman-Hill:
Never discard exceptions. Always log or display them in some way. In this particular case, just add an e.printStackTrace(), and I'll bet you'll immediately be pointed to the problem line of code.



Or recover?
 
Ernest Friedman-Hill
author and iconoclast
Posts: 24204
44
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Ken Blair:

Or recover?



Or recover. But even then, logging is prudent, most of the time.
 
Would you turn that thing down? I'm controlling a mind here! Look ... look at the tiny ad ...
The Low Tech Laboratory Movie Kickstarter is LIVE NOW!
https://www.kickstarter.com/projects/paulwheaton/low-tech
reply
    Bookmark Topic Watch Topic
  • New Topic