对java swing gui进行了一些错误检查,我正在尝试完成类的分配。 需要检查的部分是用户在计算之前输入比萨数量和苏打数量的数字。我想让程序做的是检测一个空格并提供一个弹出窗口,告诉用户该空格是空白的,需要一个0-100的数字。 我正在寻找它做任何事情,但崩溃或提供例外。一旦我们弄清楚错误检查,我就会担心弹出文本。 非常感谢任何和所有帮助.......
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();
}
答案 0 :(得分:2)
替换此行:
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 );
}