使用简单的二进制代码转换器的异常(2)

时间:2013-08-05 13:04:32

标签: java arrays exception binary decimal

我的Java(applet)中的简单二进制代码转换器出现了问题:

import java.awt.*;
import java.awt.event.*;
import java.applet.Applet;

public class dual_convert extends Applet implements ActionListener{
  TextField dual;
  TextField decimal;
  Button dual_b;
  Button decimal_b;
  Label dual_l;
  Label decimal_l;
  int decimal_i;
  String dual_s [];
  int dual_i;
  int decimal_e;

  public void init() {
    setLayout(null);

    dual = new TextField();
    dual.setBounds(90,10,100,25);
    add(dual);

    decimal = new TextField();
    decimal.setBounds(90,40,100,25);
    add(decimal);

    decimal_l = new Label("Decimal:");
    decimal_l.setBounds(10,40,80,25);
    add(decimal_l);

    dual_l = new Label("Binary:");
    dual_l.setBounds(10,10,80,25);
    add(dual_l);

    dual_b = new Button ("Convert");
    dual_b.setBounds(200,10,80,25);
    add(dual_b);
    dual_b.addActionListener(this);

    decimal_b = new Button ("Convert");
    decimal_b.setBounds(200,40,80,25);
    add(decimal_b);
    decimal_b.addActionListener(this);

  }

  public void actionPerformed (ActionEvent e) {
    if (e.getSource()==decimal_b) {
      decimal_i = Integer.parseInt(decimal.getText());  
      int ih = 0;
      dual.setText("");
      while (decimal_i>0) {
        dual.setText((decimal_i % 2)+dual.getText());
        decimal_i = decimal_i / 2; 
        ih++;
      }    
    }

    if (e.getSource()==dual_b) {
      dual_s = dual.getText().split("");
      dual_i = dual_s.length;
      decimal.setText("");
      for (int i=0;dual_i>0;dual_i--) {
        decimal_e = decimal_e + (Integer.parseInt(dual_s[dual_i-1]) * 2^i); 
        i++;
      } 
      decimal.setText(decimal_e+"");       
    } 
  }
}

通过按Convert Binary to Decimal:

抛出此异常
  

线程“AWT-EventQueue-1”中的异常   java.lang.NumberFormatException:对于输入字符串:“”at   java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)     在java.lang.Integer.parseInt(Integer.java:504)at   java.lang.Integer.parseInt(Integer.java:527)at   dual_convert.actionPerformed(dual_convert.java:65)at   java.awt.Button.processActionEvent(Button.java:409)at   java.awt.Button.processEvent(Button.java:377)at   java.awt.Component.dispatchEventImpl(Component.java:4861)at   java.awt.Component.dispatchEvent(Component.java:4687)at   java.awt.EventQueue.dispatchEventImpl(EventQueue.java:729)at at   java.awt.EventQueue.access $ 200(EventQueue.java:103)at   java.awt.EventQueue $ 3.run(EventQueue.java:688)at   java.awt.EventQueue $ 3.run(EventQueue.java:686)at   java.security.AccessController.doPrivileged(Native Method)at   java.security.ProtectionDomain $ 1.doIntersectionPrivilege(ProtectionDomain.java:76)     在   java.security.ProtectionDomain $ 1.doIntersectionPrivilege(ProtectionDomain.java:87)     在java.awt.EventQueue $ 4.run(EventQueue.java:702)at   java.awt.EventQueue $ 4.run(EventQueue.java:700)at   java.security.AccessController.doPrivileged(Native Method)at   java.security.ProtectionDomain $ 1.doIntersectionPrivilege(ProtectionDomain.java:76)     在java.awt.EventQueue.dispatchEvent(EventQueue.java:699)at   java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242)     在   java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161)     在   java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)     在   java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146)     在   java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138)     at java.awt.EventDispatchThread.run(EventDispatchThread.java:91)

对于二进制 - >十进制转换,我使用这个系统(还有其他吗?):

100101 = [(1)×25] + [(0)×24] + [(0)×23] + [(1)×22] + [(0)×21] + [(1)× 20]

100101 = [1×32] + [0×16] + [0×8] + [1×4] + [0×2] + [1×1]

100101 = 3710

https://en.wikipedia.org/wiki/Binary_number

3 个答案:

答案 0 :(得分:0)

它告诉你这一行:

decimal_i = Integer.parseInt(decimal.getText());

有一个问题,即您正在尝试将空字符串转换为整数。你必须防范这一点。

答案 1 :(得分:0)

dual_s = dual.getText().split("");

返回带有空字符串的String[]。我建议您使用char[]来存储输入字符串的每个字符,而不是像现在这样做。你可以用

来搞定
dual.getText().toCharArray();

并使用Character.digit(char ch, int radix);代替Integer.parseInt()遍历它。

答案 2 :(得分:-1)

与Splungebob的回答一样,您需要防范这种情况,但可以使用API​​完成转换。请参阅Integer.parseInt(String s, int radix)方法。