我是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
条件时第一部分(Not ==“”)将正常工作。但第二个(否则)会给出错误。
请有人告诉我该如何解决?
答案 0 :(得分:2)
如果没有数据,那么jTextField1.getText()
会给你一个空字符串(“”)和
带有空字符串的Integer.parseInt("")
会给你NumberFormatException
,因为“”不是有效数字.plus你应该检查这两个数字,所以它应该是这样的
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
阻止并显示错误
答案 1 :(得分:1)
在解析数字之前更改您的验证:
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);
}
答案 2 :(得分:1)
您应该使用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);
}