2013-02-06 63 views
-2

我创建了一个Java程序,它将十进制转换为二进制,反之亦然。我的小数点到二进制没有任何问题。但是,当我编写我的二进制到十进制我得到以下错误:Java中的异常错误

Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException: For input string: "" 
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:48) 
at java.lang.Integer.parseInt(Integer.java:470) 
at java.lang.Integer.parseInt(Integer.java:499) 
at converter.actionPerformed(converter.java:42) 
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2028) 
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2351) 
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:387) 
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:242) 
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:236) 
at java.awt.Component.processMouseEvent(Component.java:6382) 
at javax.swing.JComponent.processMouseEvent(JComponent.java:3275) 
at java.awt.Component.processEvent(Component.java:6147) 
at java.awt.Container.processEvent(Container.java:2083) 
at java.awt.Component.dispatchEventImpl(Component.java:4744) 
at java.awt.Container.dispatchEventImpl(Container.java:2141) 
at java.awt.Component.dispatchEvent(Component.java:4572) 
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4619) 
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4280) 
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4210) 
at java.awt.Container.dispatchEventImpl(Container.java:2127) 
at java.awt.Window.dispatchEventImpl(Window.java:2489) 
at java.awt.Component.dispatchEvent(Component.java:4572) 
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:704) 
at java.awt.EventQueue.access$400(EventQueue.java:82) 
at java.awt.EventQueue$2.run(EventQueue.java:663) 
at java.awt.EventQueue$2.run(EventQueue.java:661) 
at java.security.AccessController.doPrivileged(Native Method) 
at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:87) 
at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:98) 
at java.awt.EventQueue$3.run(EventQueue.java:677) 
at java.awt.EventQueue$3.run(EventQueue.java:675) 
at java.security.AccessController.doPrivileged(Native Method) 
at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:87) 
at java.awt.EventQueue.dispatchEvent(EventQueue.java:674) 
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:296) 
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:211) 
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:201) 
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:196) 
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:188) 
at java.awt.EventDispatchThread.run(EventDispatchThread.java:122) 

这里是我的代码:

import java.awt.*; 
import java.awt.event.*; 
import javax.swing.*; 

public class converter extends JFrame implements ActionListener { 

    JTextField txt1; 
    JTextField txt2; 
    JLabel lbl1; 
    JLabel lbl2; 
    JButton b1; 
    JButton b2; 

    public converter(){ 
     Container c = getContentPane(); 
     JPanel jp = new JPanel(); 
     c.add(jp); 
     jp.add(lbl1=new JLabel("Decimal: ")); 
     jp.add(txt1=new JTextField(10)); 
     jp.add(lbl2=new JLabel("Binary: ")); 
     jp.add(txt2=new JTextField(10)); 
     jp.add(b1=new JButton("Convert")); 
     jp.add(b2=new JButton("Clear")); 
     b1.addActionListener(this); 
     b2.addActionListener(this); 

    } 

    public static void main(String[] args) { 
     converter cvt = new converter(); 
     cvt.setResizable(false); 
     cvt.setVisible(true); 
     cvt.setSize(250,150); 
     cvt.setTitle("Decimal - Binary Converter"); 
    } 

    @Override 
    public void actionPerformed(ActionEvent e) { 
     // TODO Auto-generated method stub 

     String num = txt1.getText(); 
     int i = Integer.parseInt(num); 
     if(txt1 != null && e.getSource() == b1){ 
      String z = Integer.toBinaryString(i); 
      txt2.setText(z); 
     } 
     else if(e.getSource() == b2){ 
      txt1.setText(""); 
      txt2.setText(""); 
     } 
     else if(txt2 != null && e.getSource() == b1){ 
      int x = Integer.parseInt(txt2.getText().trim(), 2); 
      txt1.setText(""+x); 
     } 
    } 

} 

你能指出什么是错的?它的解决方案是什么?

回答

0

检查txt1txt2如果它是数字或不是数值。

0

跟踪的第一行显示的是,当你的程序试图为空字符串("")转换为int存在所引发的错误。如果您沿着轨迹进一步观察(在第5行),该错误出现在actionPerformed方法中。特别是,线:

if (num.length() < 1) 
    // tell user they must enter a number 
1

您没有任何界限在你的代码检查:

String num = txt1.getText(); 
int i = Integer.parseInt(num); 

你可以,如果该字符串不是空的,解决首先检查这个问题。阿卡,你有两个文本输入框和一个“转换”功能,但该功能适用​​于所有以下组合:

  • 十进制输入和数字输入都给出
  • 十进制输入和数字输入都被忽略
  • 十进制输入给出,省略二进制输入省略
  • 十进制输入,数字输入给出

你需要决定如何在四个案件都做,然后继续ÿ我们解析得当。在这些案例中,有三分之一的案例很容易处理 - 让用户不得不决定在用户​​填写十进制和二进制输入字段后再执行转换(我会建议在其中显示错误对话框那种情况)。

既然这样,你正在分析在所有情况下的十进制输入领域,其留空当这样的语句:

Integer.parseInt("") 

会抛出一个NumberFormatException,符合市场预期。


我会处理你的四种可能的情形是这样的:

public static boolean isEmpty(final String str) { 
    return (str == null || str.trim().equals("")); 
} 

final String decimalInput = text1.getText(); 
final String binaryInput = text2.getText(); 

if(! isEmpty(decimalInput)) { 
    if(! isEmpty(binaryInput)) { 
     // Decimal input and Binary input are both given, show error 
    } else { 
     // Decimal input is given, Binary input is omitted, convert to binary 
    } 
} else { 
    if(isEmpty(binaryInput)) { 
     // Decimal input and Binary input are both omitted, show error 
    } else { 
     // Decimal input is omitted, Binary input is given, convert to decimal 
    } 
} 
+0

是如此,我应该怎么办? –

+0

@ Ms.B - 我添加了一些(希望有用的)伪代码。 – Perception

0

有几件事情跳到脑海。

您可以捕获异常并显示一条消息,告诉用户它们输入的值无效。你也应该从场上得到结果,以确保。

public void actionPerformed(ActionEvent e) { 
    // TODO Auto-generated method stub 

    try {  
     String num = txt1.getText().trim(); // <-- Trim the incoming value 
     int i = Integer.parseInt(num); 
     if(txt1 != null && e.getSource() == b1){ 
      String z = Integer.toBinaryString(i); 
      txt2.setText(z); 
     } 
     else if(e.getSource() == b2){ 
      txt1.setText(""); 
      txt2.setText(""); 
     } 
     // I'm not sure if this a logic error or not, but txt2 is text field... 
     // Did you think it was the text from the field?? 
     else if(txt2 != null && e.getSource() == b1){ 
      int x = Integer.parseInt(txt2.getText().trim(), 2); 
      txt1.setText(""+x); 
     } 
    } catch (NumberFormatException exp) { 
     // Display message... 
    } 
} 

另一种是使用一个DocumentFilter以防止用户进入作为针对字段无效的任何值。

退房Text Component Featuresexamples

更新

也有一些逻辑错误......

txt1txt2从来都不可能是null,除非你已经做了一些可怕的错...

你应该检查一下按钮被按下第一个,第将允许您就如何进步做出更明确的决定。

你应该再检查从字段中的文本,并决定你想要去哪个转换路径...

try { 
    if (e.getSource() == b1) { 
     String dec = txt1.getText(); 
     String bin = txt2.getText(); 

     if (dec != null && dec.trim().length() > 0 && 
      bin != null && bin.trim().length() > 0) { 
      // Both fields are filled out?! 
     } else if (dec != null && dec.trim().length() > 0) { 
      String value = txt1.getText(); 
      int i = Integer.parseInt(dec); 
      String z = Integer.toBinaryString(i); 
      txt2.setText(z); 
     } else if (bin != null && bin.trim().length() > 0) { 
      int x = Integer.parseInt(bin, 2); 
      txt1.setText("" + x); 
     } 
    } else if (e.getSource() == b2) { 
     txt1.setText(""); 
     txt2.setText(""); 
    } 
} catch (NumberFormatException exp) { 
    exp.printStackTrace(); 
} 
+0

我试过这段代码,错误没有显示出来。但我的问题是,当我在我的txt2字段中输入二进制数时,txt1不显示十进制值。 –

+0

你也有一些逻辑问题(我的'try-catch'有点宽) - 检查更新 – MadProgrammer