gpt4 book ai didi

Java gui 空格错误检查

转载 作者:行者123 更新时间:2023-12-02 05:11:47 24 4
gpt4 key购买 nike

对 java swing gui 进行了一些错误检查,我正在尝试完成类作业。需要检查的部分是用户在计算之前输入披萨数量和苏打水数量的数字的地方。我想让程序做的是检测空格并提供一个弹出窗口,告诉用户该空格是空白并且需要 0-100 之间的数字。我正在寻找它做任何事情,除了崩溃或给出异常(exception)。一旦我们搞清楚了错误检查,我就会担心弹出窗口的文本。非常感谢任何和所有的帮助......

private void btnCalcActionPerformed(java.awt.event.ActionEvent evt) {                                        
final double LOCAL_TAX = .02;
final double STATE_TAX = .06;
NumberFormat nf = NumberFormat.getCurrencyInstance();

int intPizza = Integer.parseInt(qtyInputPizza.getText());
while (!intPizza.hasNextInt(101))
{

}
int intSoda = Integer.parseInt(qtyInputSoda.getText());

double dblPizzaSub = intPizza * PIZZA_PRICE;
double dblSodaSub = intSoda * SODA_PRICE;

pizzaSubtotal.setText(nf.format(dblPizzaSub));
sodaSubtotal.setText(nf.format(dblSodaSub));

double dblNetSale = dblPizzaSub + dblSodaSub;
netSaleValue.setText(nf.format(dblNetSale));

double dblStateTax = dblNetSale * STATE_TAX;
double dblLocalTax = dblNetSale * LOCAL_TAX;
stateTaxValue.setText(nf.format(dblStateTax));
localTaxValue.setText(nf.format(dblLocalTax));

double dblTotalSale = dblNetSale + dblStateTax + dblLocalTax;
totalSaleValue.setText(nf.format(dblTotalSale));

btnClear.requestFocusInWindow();
}

最佳答案

替换这一行:

int intPizza = Integer.parseInt(qtyInputPizza.getText());

String strPizza = qtyInputPizza.getText();
int intPizza = 0;
if ( strPizza == null || strPizza.trim().length() == 0 ) {
// Display your error message
}
else
{
// You can add a test to make sure it is an int not something else
ntPizza = Integer.parseInt( strPizza );
}

关于Java gui 空格错误检查,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27275586/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com