I am making a pet veterinary system that has a payment transaction like this. Whenever I tried to input on 1 jtextfield and calculate it while leave other jtextfield empty, error pops up : Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException: empty String
Here is the attached payment transaction menu form
How can I get the input and calculate it while ignoring the other text field empty (because for example the customer didn't get the other services, only the consultation service)
Below is my attached code :
double Qty1 = Double.parseDouble(qty_consult.getText());
double Qty2 = Double.parseDouble(qty_dogvac.getText());
double Qty3 = Double.parseDouble(qty_catvac.getText());
double Qty4 = Double.parseDouble(qty_antirabies.getText());
double Qty5 = Double.parseDouble(qty_deworm.getText());
double Qty6 = Double.parseDouble(qty_blood.getText());
double Qty7 = Double.parseDouble(qty_urinalysis.getText());
double Qty8 = Double.parseDouble(qty_skin.getText());
double addup, subTotal, subtotal,tax_ratefee, fee1, fee2, fee3, fee4, fee5, fee6, fee7, fee8;
String consult_fee = String.format("%.2f", price_consultfee);
price_consult.setText(consult_fee);
String vac_fee = String.format("%.2f", price_vacfee);
price_vac.setText(vac_fee);
String vac2_fee = String.format("%.2f", price_vac2fee);
price_vac2.setText(vac2_fee);
String rabies_fee = String.format("%.2f", price_rabiesfee);
price_rabies.setText(rabies_fee);
String deworm_fee = String.format("%.2f", price_dewormfee);
price_deworm.setText(deworm_fee);
String cbc_fee = String.format("%.2f", price_cbcfee);
price_cbc.setText(cbc_fee);
String urine_fee = String.format("%.2f", price_urinefee);
price_urine.setText(urine_fee);
String skin_fee = String.format("%.2f", price_skinfee);
price_skin.setText(skin_fee);
fee1 = Qty1 * price_consultfee;
fee2 = Qty2 * price_vacfee;
fee3 = Qty3 * price_vac2fee;
fee4 = Qty4 * price_rabiesfee;
fee5 = Qty5 * price_dewormfee;
fee6 = Qty6 * price_cbcfee;
fee7 = Qty7 * price_urinefee;
fee8 = Qty8 * price_skinfee;
String sub1 = String.format("%.2f", fee1);
sub_consult.setText(sub1);
String sub2 = String.format("%.2f", fee2);
sub_vac.setText(sub2);
String sub3 = String.format("%.2f", fee3);
sub_vac2.setText(sub3);
String sub4 = String.format("%.2f", fee4);
sub_anti.setText(sub4);
String sub5 = String.format("%.2f", fee5);
sub_deworming.setText(sub5);
String sub6 = String.format("%.2f", fee6);
sub_cbc.setText(sub6);
String sub7 = String.format("%.2f", fee7);
sub_urine.setText(sub7);
String sub8 = String.format("%.2f", fee8);
sub_skin.setText(sub8);
subTotal = fee1 + fee2+ fee3+ fee4 + fee5 + fee6 + fee7 + fee8;
tax_ratefee = (subTotal * taxrate)/100;
//========================Tax=========================//
String subTax = String.format("%.2f", tax_ratefee);
txt_tax.setText(subTax);
//======================SubTotal======================//
String subTotalAll = String.format("%.2f", subTotal);
sub_total.setText(subTotalAll);
//====================OverallTotal=====================//
addup = subTotal + tax_ratefee;
String total = String.format("%.2f", addup);
txt_total.setText(total);
//=====================DateandTime=======================//
Calendar timer = Calendar.getInstance();
timer.getTime();
SimpleDateFormat intime = new SimpleDateFormat ("HH:mm:ss");
txt_time.setText(intime.format(timer.getTime()));
SimpleDateFormat indate = new SimpleDateFormat ("dd-MMM-yyyy");
txt_date.setText(indate.format(timer.getTime()));
I tried doing this :
if (qty_consult.getText().equals("")){
JOptionPane.showMessageDialog(null,"Empty Field/s");
}else {
double Qty1 = Double.parseDouble(qty_consult.getText());
fee1 = Qty1 * price_consultfee;
String consult_fee = String.format("%.2f", price_consultfee);
price_consult.setText(consult_fee);
String sub1 = String.format("%.2f", fee1);
sub_consult.setText(sub1);
subTotal = fee1;
tax_ratefee = (subTotal * taxrate)/100;
//========================Tax=========================//
String subTax = String.format("%.2f", tax_ratefee);
txt_tax.setText(subTax);
//======================SubTotal======================//
String subTotalAll = String.format("%.2f", subTotal);
sub_total.setText(subTotalAll);
//====================OverallTotal=====================//
addup = subTotal + tax_ratefee;
String total = String.format("%.2f", addup);
txt_total.setText(total);
//=====================DateandTime=======================//
Calendar timer = Calendar.getInstance();
timer.getTime();
SimpleDateFormat intime = new SimpleDateFormat ("HH:mm:ss");
txt_time.setText(intime.format(timer.getTime()));
SimpleDateFormat indate = new SimpleDateFormat ("dd-MMM-yyyy");
txt_date.setText(indate.format(timer.getTime()));
and it calculated the input perfectly and after that error of empty string pop-ups again. Am I doing something wrong here?
when working with user input you have to be ready to expect anything, the user always find a way to break your code, so I would recommend you to create a kind of a utility method for accessing and parsing the input. For instance:
private static double getNumberInput(JTextField field, double defaultValue){
String textInput = field.getText();
//Sorry, don't recall if getText can return a null value
//just making sure that we dont have a NullPointerException
//this part can be removed if getText never returns null
texInput = textInput == null? "" : textInput.trim(); //Make sure you trim the value
if("".equals(textInput)){
return defaultValue;
}
try{
return Double.parse(textInput);
}catch(NumberFormatException ex){
//Invalid input
return defaultValue;
}
}
This way you can reuse the method and be sure that when you are trying to get the value inside the field, you always have a value.
double Qty1 = getNumberInput(qty_consult, 0f);
double Qty2 = getNumberInput(qty_dogvac, 0f);
double Qty3 = getNumberInput(qty_catvac, 0f);
This way you'll never have a java.lang.NumberFormatException again.