2013-08-04 70 views
0

我在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]) * 2^i); 
     i++; 
     } 
     decimal.setText(decimal_e+"");  
    } 
    } 
} 

它按二進制轉換爲十進制拋出該異常

在異常線程 「AWT-EventQueue的-1」 java.lang.ArrayIndexOutOfBoundsException:8在 dual.actionPerformed(dual.java:65)在 java.awt.Button.processActionEvent(Button.java:409)在 java.awt.Button.processEvent(但(Event.ueva.Tool.java:377)at java.awt.Component.dispatchEventImpl(Component.java:4861)at java.awt.Component.dispatchEvent(Component.java:4687)at java.awt.EventQueue.dispatchEventImpl(EventQueue。 (EventQueue.java:103)at java.awt.EventQueue $ 3.run的.java:686)在 java.security.AccessController.doPrivileged(本機方法)在 java.security.ProtectionDomain $ 1.doIntersectionPrivilege(ProtectionDomain.java:76) 在 java.security.ProtectionDomain $ 1.doIntersectionPrivilege(ProtectionDomain.java :87) at 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) at 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) at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146) at java。 awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138) 在java.awt.EventDispatchThread.run(EventDispatchThread.java:91)

對於二元 - >十進制轉換我用這個系統(IS甚至有其他?):

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

編輯:後塔拉斯張貼有一個新的例外:在螺紋

異常 「AWT-EventQueue的-1」 java.lang.NumberFormatException:對於輸入字符串:「 「在 java.lang.NumberFormatException.forInputString(NumberFormatException.java:65) at java.lang.Integer.parseInt(Integer.java:504)at java.lang.Integer.parseInt(Integer.java:527)at dual_convert.actionPerformed(dual_convert。java:64)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 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(本機方法) 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:702)at java.awt.EventQueue $ 4.run(EventQueue.java:700)at java。 java.security.ProtectionDomain $ 1.doIntersectionPrivilege(ProtectionDomain.java:76) at 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) 在java.awt.EventDispatchThread.run(EventDispatchThread.java:91)

回答

0

的問題是在這裏:

for (int i=0;dual_i>0;dual_i--) { 
    decimal_e = decimal_e + (Integer.parseInt(dual_s[dual_i - 1]) * 2^i); 
    i++; 
    } 

您應該使用dual_s[dual_i - 1]代替dual_s[dual_i]因爲dual_i = dual_s.length;和數組索引開始0

+0

我們正在前進......新的例外: [http://pastebin.com/bmqf8SrE](http://pastebin.com/bmqf8SrE) – user2651027

+0

這個問題呢?我是否修復了您的異常? – Tala

+0

舊的,是的,但有一個新的來了。 – user2651027

0

您既擁有該類,又擁有一個名爲dual的文本框。我對這個編譯有點驚訝,但這可能是問題所在。

+0

你分辯,這是奇怪的,但它的工作原理,甚至與其他班級名稱。 //編輯我已更改帖子中的代碼。 – user2651027