代码中的数字格式异常错误

时间:2015-04-14 20:25:13

标签: java numberformatexception jformattedtextfield

我的代码中出现错误,说明数字格式异常错误。 我试图获取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);
}

}

1 个答案:

答案 0 :(得分:0)

更改此行:

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

要:

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

或者:

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