我是Java
的新手。我最近开始了我的第一个项目(计算器)在Net beans。
当我在工作的时候,我给它添加了一个if
条件。if
条件检查字段是否为空。
这是我的代码:
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
double a, b, c;
a = Integer.parseInt(jTextField1.getText());
b = Integer.parseInt(jTextField2.getText());
if(jTextField1.getText().equals("")) {
jLabel2.setText("Please enter a number");
}
else {
c = a + b;
jLabel2.setText("" + c);
}
}
当我点击带有空字段的按钮时,它会给我一个这样的错误:
run:
Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException: For input string: ""
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
at java.lang.Integer.parseInt(Integer.java:504)
at java.lang.Integer.parseInt(Integer.java:527)
at calc.jButton1ActionPerformed(calc.java:132)
at calc.access$000(calc.java:11)
at calc$1.actionPerformed(calc.java:53)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2018)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2341)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
at java.awt.Component.processMouseEvent(Component.java:6505)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3320)
at java.awt.Component.processEvent(Component.java:6270)
at java.awt.Container.processEvent(Container.java:2229)
at java.awt.Component.dispatchEventImpl(Component.java:4861)
at java.awt.Container.dispatchEventImpl(Container.java:2287)
at java.awt.Component.dispatchEvent(Component.java:4687)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4832)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4492)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4422)
at java.awt.Container.dispatchEventImpl(Container.java:2273)
at java.awt.Window.dispatchEventImpl(Window.java:2719)
at java.awt.Component.dispatchEvent(Component.java:4687)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:735)
at java.awt.EventQueue.access$200(EventQueue.java:103)
at java.awt.EventQueue$3.run(EventQueue.java:694)
at java.awt.EventQueue$3.run(EventQueue.java:692)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87)
at java.awt.EventQueue$4.run(EventQueue.java:708)
at java.awt.EventQueue$4.run(EventQueue.java:706)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:705)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:91)
但是当我将if(jTextField1.getText().equals(""))
改为if(!jTextField1.getText().equals(""))
时,它可以正常工作,但是当我将else
条件添加到它的第一部分(不== ")将正常工作。但是第二个(else)会给出错误。
谁能告诉我怎么修理它?
如果没有数据,那么jTextField1.getText()
将给您一个空字符串(")和带空字符串的Integer.parseInt("")
会给你NumberFormatException
因为"不是一个有效的数字。另外你应该检查两个数字所以它应该像这样
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
double a, b, c;
if (jTextField1.getText().equals("") || jTextField2.getText().equals("")){
jLabel2.setText("Missing Input");
}else{
try{
a = Integer.parseInt(jTextField1.getText());
b = Integer.parseInt(jTextField2.getText());
c = a + b;
jLabel2.setText("" + c);
}catch(NumberFormatException e){
jLabel2.setText("Enter Valid numbers");
}
}
}
简单地把你的代码放在try
中,所以如果任何输入不是一个有效的数字,那么控制到catch
块并显示错误
将验证更改为在解析数字之前进行:
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
int a, b, c;
if(jTextField1.getText().equals("")) {
jLabel2.setText("Please enter a number");
}
else {
a = Integer.parseInt(jTextField1.getText());
b = Integer.parseInt(jTextField2.getText());
c = a + b;
jLabel2.setText("" + c);
}
}
您可能还需要检查其他条件,例如是否输入数字。将文本放在那里也会抛出NumberFormatException。我会将您的验证逻辑更改为如下内容:
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
int a, b, c;
try {
a = Integer.parseInt(jTextField1.getText());
} catch (NumberFormatException e) {
jLabel2.setText("Please enter a number in field1");
return;
}
try {
b = Integer.parseInt(jTextField2.getText());
} catch (NumberFormatException e) {
jLabel2.setText("Please enter a number in field2");
return;
}
c = a + b;
jLabel2.setText("" + c);
}
你应该用try和catch块来包围你的代码。如果文本字段为空,则不能将其文本解析为整数。
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
int sum = 0;
int a, b;
try {
a = Integer.parseInt(jTextField1.getText());
sum += a;
} catch (NumberFormatException e) {
System.out.println("Please correct your text field number 1");
}
try {
b = Integer.parseInt(jTextField2.getText());
sum += b;
} catch (NumberFormatException e) {
System.out.println("Please correct your text field number 2");
}
jLabel.setText("" + sum);
}