2013-02-06 120 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