Je rencontre une erreur dans mon code qui indique cette erreur d'exception de format de nombre. J'essaie de prendre l'adresse IP ex.172.16.10.100 & masque de sous-réseau 255.255.255.0. Après avoir pris la valeur, j'essaie de les convertir en format décimal au format binaire. Je reçois erreur dans cette ligne IPaddress = IPaddress + printBinaryFormat(octet[i]);
Erreur d'exception de format de nombre dans le code
Pourriez-vous s'il vous plaît me suggérer comment résoudre ce problème?
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);
}
}
Pouvez-vous annonce d la trace complète de la pile à votre question? – RealSkeptic
Veuillez réduire votre code à la partie nécessaire au lieu de simplement tout poster. – Turing85
essayez d'imprimer 'txtIP.getText()' dans le journal. vérifier si la valeur contient un autre caractère que le point (.) et les nombres. Il semble qu'il contient des espaces qui se traduisent par un 'numberformatexception' – Razib