2015-04-14 64 views
1

我的代碼中出現錯誤,表明數字格式異常錯誤。 我想取IP地址ex.172.16.10.100 &子網掩碼255.255.255.0。 取值後,我試圖將它們轉換爲十進制格式。 我在這一行中得到錯誤IPaddress = IPaddress + printBinaryFormat(octet[i]);代碼中的數字格式異常錯誤

請問請問我該怎麼解決這個問題?

import java.awt.*; 
import java.awt.event.*; 
import javax.swing.text.MaskFormatter; 
import javax.swing.*; 
import java.text.ParseException; 
import java.util.regex.Pattern; 
import java.math.BigInteger; 
import java.util.Arrays; 
import java.util.StringTokenizer; 

public class update extends JPanel implements ActionListener 
{ 
private JButton cmdCalculate; 
public JFormattedTextField txtIP, txtSub;//Object name txtIP,txtSub 
private JLabel lblCalculate, lblIP, lblSub, lblNetwork, lblHost; 
private static String IPaddress = "",Subenetaddress= "", Networkaddress= "", Lastaddress= ""; 
private JPanel panAnswerArea, panNorthArea, panBase, panAddressGrid, panIP, panSub, panButton; 

public update() 
{ 

    super.setLayout(new BorderLayout());  
    cmdCalculate = new JButton("Calculate"); 
    cmdCalculate.addActionListener(this); 
    lblIP = new JLabel("IP Address: "); 
    lblSub = new JLabel("Subnet Mask: "); 
    panIP = new JPanel(); 
    panSub = new JPanel(); 
    panIP.add(lblIP); 
    panSub.add(lblSub); 
    MaskFormatter mf = null; 

    try { 
     mf = new MaskFormatter("***.***.***.***"); 

    } catch (ParseException e) { 
     e.printStackTrace(); 
    } 

    txtIP = new JFormattedTextField(mf); 
    panIP.add(txtIP); 
    txtIP.setFont(new Font(Font.MONOSPACED, Font.PLAIN, 12)); 
    txtIP.setPreferredSize(new Dimension(150, 24)); 


    txtSub = new JFormattedTextField(mf); 
    panSub.add(txtSub); 
    txtSub.setFont(new Font(Font.MONOSPACED, Font.PLAIN, 12)); 
    txtSub.setPreferredSize(new Dimension(150, 24)); 

    lblCalculate = new JLabel("Calculate Network and Host parts: ", JLabel.LEFT); 
    lblNetwork = new JLabel("Network address: ", JLabel.LEFT); 
    lblHost = new JLabel("Host address: ", JLabel.LEFT); 

    lblNetwork.setText("Network address: "); 
    lblHost.setText("Host address: "); 

    panAnswerArea = new JPanel(new BorderLayout()); //Stores the network and host addresses 
    panNorthArea = new JPanel(new BorderLayout()); //North has the base radio buttons, the south has the IP/Subnet 
    panAddressGrid = new JPanel(new GridLayout(4,2)); //Stores the IP, subnet address and the calculate button 
    panButton = new JPanel(); 


    panAnswerArea = new JPanel(new BorderLayout()); //Stores the network and host addresses 
    this.add(panAnswerArea, BorderLayout.SOUTH); 
    panAnswerArea.add(lblNetwork, BorderLayout.NORTH); 
    panAnswerArea.add(lblHost, BorderLayout.SOUTH); 

    panNorthArea = new JPanel(new BorderLayout()); 
    this.add(panNorthArea, BorderLayout.NORTH); 
    panNorthArea.add(panAddressGrid, BorderLayout.SOUTH); 
    panAddressGrid.add(panIP); 
    panAddressGrid.add(panSub); 
    panAddressGrid.add(panButton); 
    panButton.add(lblCalculate); 
    panButton.add(cmdCalculate); 
} 
public void actionPerformed (ActionEvent e) 
{ 
    String strIP= ""; 
    String strSub= ""; 
    String IPaddress = "",Subenetaddress= "", Networkaddress= "", Lastaddress= "", inversbits= "",inversesubnetmask= ""; 
    strIP = txtIP.getText(); 
    strSub = txtSub.getText(); 

    StringTokenizer st; 
    String[]octet=new String[4]; 
    st = new StringTokenizer(strIP,"."); 

    for(int i =0;i!=4;i++) 
    { 
     octet[i]=st.nextToken(); 
     IPaddress = IPaddress + printBinaryFormat(octet[i]);//problem is here 


    } 


} 

private static String printBinaryFormat(String number) 
{ 
    int decNum = Integer.parseInt(number),length = 0; 
    String binary = "", answer = ""; 

    while(decNum > 0){ 

     binary += Integer.toString (decNum %2); 

     decNum= decNum/2; 

    } 
    length = binary.length(); 

    for (int padding = (8 - length); padding > 0; padding--) 
    { 
     answer += "0"; 
    } 

    for (int i = length; i > 0; i--) 
    { 
     answer += binary.charAt(i - 1); 
    } 

    return answer; 
} 
public static void main(String[] args) 
{ 
    JFrame.setDefaultLookAndFeelDecorated(true);// decorated the border 
    JFrame frame = new JFrame("Subnet Calculators"); //Title 
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    //frame.setSize(500, 600); 

    JComponent paneMain = new update(); 
    paneMain.setOpaque(true); 
    paneMain.setPreferredSize(new Dimension(500, 300));// It works of the size of the frame 
    frame.setContentPane(paneMain); 
    frame.pack(); 
    frame.setVisible(true); 
} 

}

+0

你能否廣告d完整的堆棧跟蹤到你的問題? – RealSkeptic

+0

請將您的代碼修剪到必要的部分,而不是隻發佈一切。 – Turing85

+0

嘗試在日誌中打印'txtIP.getText()'。檢查值是否包含除點(。)和數字以外的其他任何字符。它似乎包含一些空格,導致'numberformatexception' – Razib

回答

0

改變這一行:

int decNum = Integer.parseInt(number), length = 0; 

要:

int decNum = Integer.parseInt(number.trim()), length = 0; 

或者:

int decNum = Integer.parseInt(number.replaceAll(" ", "")), length = 0; 
+0

偉大的建議!它的工作。感謝您的幫助。 – jisan