java - JOptionPane.showMessageDialog keeps reappearing -
my code supposed ensure users enter binary string. goes right , in order if enter correct binary number (1, 0). when enter wrong number (alphabet or number other 0 , 1) joptionpane.showmessagedialog appears showing error message, , when click "ok" or "cross" button reappears again. close app have use process manager kill "java.exe".
here complete code app:
import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.util.*; class bn extends jframe { private jlabel l1; private jlabel l2; private jlabel l3; private jtextfield tf; private jbutton oc; private jbutton hd; private jbutton dc; public bn() { settitle("binary conversion"); setsize(350 , 190); setresizable(false); setlocation(720 , 200); //setdefaultcloseoperation(jframe.exit_on_close); setlayout(new flowlayout(flowlayout.center, 25, 10)); l1 = new jlabel(">>>>>>>>binary conversion<<<<<<<<"); l2 = new jlabel("enter number:"); l3 = new jlabel(" convert to: "); tf = new jtextfield(25); oc = new jbutton("octal"); hd = new jbutton("hexa decimal"); dc = new jbutton("decimal"); add(l1); // binary conversion add(l2); // enter number add(tf); // text field add(l3); // conver add(oc); // octal add(hd); // hexa decimal add(dc); // decimal oc.addactionlistener(new cvr()); hd.addactionlistener(new cvr()); dc.addactionlistener(new cvr()); setvisible(true); } void res() { int b = 0; string num = tf.gettext(); int , l; l = num.length(); char ch; { (i=0 ; i<l ; i++) { b = 0; ch = num.charat(i); if (character.isdigit(ch) && (ch == 48 || ch == 49)) b=1; else { joptionpane.showmessagedialog(null, "please enter binary number (1 , 0)"); } } } while(b != 1); } void occ() { res(); string num = tf.gettext(); long dec = long.parselong(num,2); string oct = long.tooctalstring(dec); joptionpane.showmessagedialog(null, "octal equivalent is: "+ oct); } void hdc() { res(); string num = tf.gettext(); long dec = integer.parseint(num,2); string hex = long.tohexstring(dec); joptionpane.showmessagedialog(null, "hexa decimal equivalent is: "+ hex); } void dcc() { res(); string num = tf.gettext(); long decimal = 0, temp, = 0; temp = long.parselong(num); while (temp != 0) { long r = temp % 10; long value = r * (int)math.pow(2, i); i++; decimal = decimal + value; temp /= 10; } joptionpane.showmessagedialog(null, "decimal equivalent is: "+ decimal); } private class cvr implements actionlistener { public void actionperformed(actionevent e) { if (e.getsource() == oc) { occ(); } if (e.getsource() == hd) { hdc(); } if (e.getsource() == dc) { dcc(); } } } public static void main(string args[]) { bn obj = new bn(); } }
the code joptionpane located in void res()
method. how can close dialog pane may reenter value input?
remove { } while loop
do { (i=0 ; < l ; i++) { b = 0; ch = num.charat(i); if (character.isdigit(ch) && (ch == 48 || ch == 49)) b=1; else { joptionpane.showmessagedialog(null, "please enter binary number (1 , 0)"); } } } while(b != 1);
Comments
Post a Comment