微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

每当用户从文本字段输入内容时如何计算输入

如何解决每当用户从文本字段输入内容时如何计算输入

我正在制作一个宠物兽医系统,它有这样的付款交易。 每当我尝试在 1 个 jtextfield 上输入并计算它时,同时将其他 jtextfield 留空,就会弹出错误: 线程“AWT-EventQueue-0”中的异常java.lang.NumberFormatException:空字符串

这是附加的付款交易菜单表格

Payment Transaction Menu

如何在忽略其他文本字段为空的情况下获取输入并计算它(因为例如客户没有获得其他服务,只有咨询服务)

下面是我附上的代码

    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()));
    
    
    
    
  

我尝试这样做:

     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",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()));

它完美地计算了输入,然后再次弹出空字符串错误。我在这里做错了吗?

解决方法

在处理用户输入时,您必须准备好期待任何事情,用户总是会找到一种方法来破坏您的代码,因此我建议您创建一种用于访问和解析输入的实用方法。例如:

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;
    }
}

这样您就可以重用该方法,并确保当您尝试获取字段内的值时,您始终拥有一个值。

double Qty1 = getNumberInput(qty_consult,0f);
double Qty2 = getNumberInput(qty_dogvac,0f);
double Qty3 = getNumberInput(qty_catvac,0f);

这样你就不会再有 java.lang.NumberFormatException 了。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。