有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

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

我正在制作一个宠物兽医系统,它有这样的支付交易。 每当我试图输入1个jtextfield并计算它,而另一个jtextfield为空时,就会弹出错误: java线程“AWT-EventQueue-0”中出现异常。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", 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()));

它完美地计算了输入,然后再次出现空字符串错误。我做错什么了吗


共 (1) 个答案

  1. # 1 楼答案

    当使用用户输入时,你必须准备好接受任何东西,用户总是能找到破解代码的方法,所以我建议你创建一种实用的方法来访问和解析输入。例如:

    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