【问题标题】:Exception with a simple binary code converter (2)一个简单的二进制代码转换器的异常 (2)
【发布时间】:2013-08-05 13:04:31
【问题描述】:

我在 Java(小程序)中使用简单的二进制代码转换器时遇到了问题:

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:对于输入字符串:“”在 java.lang.NumberFormatException.forInputString(NumberFormatException.java:65) 在 java.lang.Integer.parseInt(Integer.java:504) 在 java.lang.Integer.parseInt(Integer.java:527) 在 dual_convert.actionPerformed(dual_convert.java:65) 在 java.awt.Button.processActionEvent(Button.java:409) 在 java.awt.Button.processEvent(Button.java:377) 在 java.awt.Component.dispatchEventImpl(Component.java:4861) 在 java.awt.Component.dispatchEvent(Component.java:4687) 在 java.awt.EventQueue.dispatchEventImpl(EventQueue.java:729) 在 java.awt.EventQueue.access$200(EventQueue.java:103) 在 java.awt.EventQueue$3.run(EventQueue.java:688) 在 java.awt.EventQueue$3.run(EventQueue.java:686) 在 java.security.AccessController.doPrivileged(Native Method) 在 java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76) 在 java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87) 在 java.awt.EventQueue$4.run(EventQueue.java:702) 在 java.awt.EventQueue$4.run(EventQueue.java:700) 在 java.security.AccessController.doPrivileged(Native Method) 在 java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76) 在 java.awt.EventQueue.dispatchEvent(EventQueue.java:699) 在 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) 在 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

【问题讨论】:

  • 我给你一个提示,让你做调试。错误消息NumberFormatException: For input string: "" 表示您正在尝试将空字符串转换为数字。这行不通。
  • 这很清楚,但即使我在双TextField中写了一些东西,它也不应该是空的。
  • 好吧,我发现了问题: split("") 不会创建零,所以当 for 循环到达 dual_i=0 时,它会抛出异常。我不得不将条件更改为 dual_i>1。

标签: java arrays exception binary decimal


【解决方案1】:

这个

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

返回带有前导空字符串的String[]。我建议您使用char[] 来存储输入字符串的每个字符,而不是按照您现在的方式进行操作。你可以得到它

dual.getText().toCharArray();

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

【讨论】:

    【解决方案2】:

    它告诉你这一行:

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

    有一个问题,即您试图将空白字符串转换为整数。你必须提防这种情况。

    【讨论】:

    • 第 65 行是 'decimal_e = decimal_e + (Integer.parseInt(dual_s[dual_i-1]) * 2^i);'不是你发布的那条线。
    • @user2651027:那么(显然)dual_s[dual_i - 1] 是一个空字符串。
    • 这个想法是你的电话,Integer.parseInt(someText) someText 是空白的。
    【解决方案3】:

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

    【讨论】:

    • 不,当s 为空字符串时,此方法如何提供帮助?
    • @jlordo 也许我的措辞模棱两可,我现在已经编辑过了。我的意思是仍然需要输入验证,但是 parseInt 支持从 base 2 到 base 10 的实际转换。
    猜你喜欢
    • 2013-08-05
    • 2013-07-08
    • 2012-06-06
    • 1970-01-01
    • 1970-01-01
    • 2012-07-31
    • 2013-11-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多