java - exception handing -
i created program should let user enter loan amount , loan period in number of years text fields, , should display monthly , total payments each interest rate starting 5 percent 8 percent, increments of one-eighth, in text area. may sound stupid not sure how add exception handling add exception handling when non-numeric value entered.for example instead of entering 5 number of years, user enters five.the application should display error message. in advance. package loan;
import javafx.application.application; import javafx.scene.scene; import javafx.stage.stage; import javafx.scene.control.label; import javafx.scene.control.textfield; import javafx.scene.control.textarea; import javafx.scene.control.button; import javafx.scene.control.scrollpane; import javafx.scene.layout.hbox; import javafx.scene.layout.borderpane; import javafx.geometry.pos; public class loan extends application { protected textfield tfloanamount = new textfield(); protected textfield tfnumberofyears = new textfield(); protected textarea textarea = new textarea(); @override // override start method in application class public void start(stage primarystage) { tfnumberofyears.setprefcolumncount(2); tfloanamount.setprefcolumncount(7); textarea.setprefcolumncount(30); // create button button btshowtable = new button("show table"); // create hbox hbox paneforcontrols = new hbox(10); paneforcontrols.setalignment(pos.center); paneforcontrols.getchildren().addall(new label("loan amount"), tfloanamount, new label("number of years"), tfnumberofyears, btshowtable); // create scrollpane scrollpane scrollpane = new scrollpane(textarea); // create pane borderpane pane = new borderpane(); pane.settop(paneforcontrols); pane.setcenter(textarea); // create , register handler btshowtable.setonaction(e -> { print(); }); // create scene , place in stage scene scene = new scene(pane); primarystage.settitle("loans"); // set stage title primarystage.setscene(scene); // place scene in stage primarystage.show(); // display stage } private void print() { // create output string string output = ""; double monthlyinterestrate; // monthly interest rate double monthlypayment; // monthly payment // add table header output += "interest rate monthly payment total payment\n"; // calculate , add table interest rates output (double = 5.0; <= 8; += 0.125) { monthlyinterestrate = / 1200; monthlypayment = double.parsedouble(tfloanamount.gettext()) * monthlyinterestrate / (1 - 1 / math.pow(1 + monthlyinterestrate, double.parsedouble(tfnumberofyears.gettext()) * 12)); output += string.format("%-24.3f%-34.2f%-8.2f\n", i, monthlypayment, (monthlypayment * 12) * double.parsedouble(tfnumberofyears.gettext())); } textarea.settext(output); } public static void main(string[] args) { launch(args); } }
textfield(tfnumberofyears) [textfield]: https://docs.oracle.com/javase/8/javafx/api/javafx/scene/control/textfield.html, textfield has method public final string gettext()
, method returns string.
when use [double.parsedouble(tfnumberofyears.gettext())
]: https://docs.oracle.com/javase/7/docs/api/java/lang/double.html
public static double parsedouble(string s) throws numberformatexception
throws:
nullpointerexception - if string null
numberformatexception - if string not contain parsable double.
so can put code inside try/catch block , make desire when user enters 5 instead of 5.
like:
`private void print() { // create output string string output = ""; double monthlyinterestrate; // monthly interest rate double monthlypayment; // monthly payment // add table header output += "interest rate monthly payment total payment\n"; // calculate , add table interest rates output (double = 5.0; <= 8; += 0.125) { monthlyinterestrate = / 1200; try{ monthlypayment = double.parsedouble(tfloanamount.gettext()) * monthlyinterestrate / (1 - 1 / math.pow(1 + monthlyinterestrate, double.parsedouble(tfnumberofyears.gettext()) * 12)); } catch(numberformatexception e){ //here write code manage exception } try{ output += string.format("%-24.3f%-34.2f%-8.2f\n", i, monthlypayment, (monthlypayment * 12) * double.parsedouble(tfnumberofyears.gettext())); } catch(numberformatexception e){ //here write code manage exception } } textarea.settext(output); } public static void main(string[] args) { launch(args); }`
this example of how handle exception.
Comments
Post a Comment