java - Number format exception error in the code -
i having error in code state number format exception error. trying take ip address ex.172.16.10.100 & subnet mask 255.255.255.0. after taking value, trying convert them decimal binary format. getting error in line ipaddress = ipaddress + printbinaryformat(octet[i]);
could please suggest me how can sorted out problem?
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 , 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 network , host addresses pannortharea = new jpanel(new borderlayout()); //north has base radio buttons, south has ip/subnet panaddressgrid = new jpanel(new gridlayout(4,2)); //stores ip, subnet address , calculate button panbutton = new jpanel(); pananswerarea = new jpanel(new borderlayout()); //stores network , 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 =0;i!=4;i++) { octet[i]=st.nexttoken(); ipaddress = ipaddress + printbinaryformat(octet[i]);//problem 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(); (int padding = (8 - length); padding > 0; padding--) { answer += "0"; } (int = length; > 0; i--) { answer += binary.charat(i - 1); } return answer; } public static void main(string[] args) { jframe.setdefaultlookandfeeldecorated(true);// decorated 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));// works of size of frame frame.setcontentpane(panemain); frame.pack(); frame.setvisible(true); }
}
change line:
int decnum = integer.parseint(number), length = 0;
to:
int decnum = integer.parseint(number.trim()), length = 0;
or:
int decnum = integer.parseint(number.replaceall(" ", "")), length = 0;
Comments
Post a Comment