代码中数字格式异常错误

Number format exception error in the code

我的代码有错误,指出数字格式异常错误。 我正在尝试使用 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);
}

}

更改此行:

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

收件人:

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

或者:

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