java - Continuing a loop until good input? -
i'm trying make program user needs account access other parts of it. want set if user's 2 confirm password dont match, have reenter info. also, if user leaves blank, must reenter info. how can continues loop until user enters info?
if (e.getsource() == okbutton) { if(!passstring.equals(passstringconfirm) || username.equals(null) || passstring.equals(null) || passstringconfirm.equals(null)){ enterusername.settext(""); enterpassword.settext(""); enterconfirmpassword.settext(""); } }
this have far, , if works 1 iteration. tried while , continually print out warning message trying print out in joptionpane.
import java.awt.color; import java.awt.flowlayout; import java.awt.event.actionevent; import java.awt.event.actionlistener; import javax.swing.borderfactory; import javax.swing.jbutton; import javax.swing.jframe; import javax.swing.jlabel; import javax.swing.joptionpane; import javax.swing.jpasswordfield; import javax.swing.jtextfield; import javax.swing.border.border; public class createaccount extends jframe implements actionlistener { jlabel username = new jlabel("enter username"); jtextfield enterusername = new jtextfield(null, 15); jlabel password = new jlabel("enter password"); jpasswordfield enterpassword = new jpasswordfield(null, 15); jlabel passwordconfirm = new jlabel("confirm password."); jpasswordfield enterconfirmpassword = new jpasswordfield(null, 15); jbutton okbutton = new jbutton("ok"); string username; double initialdeposit; public createaccount() { add(username); add(enterusername); add(password); add(enterpassword); add(passwordconfirm); add(enterconfirmpassword); add(okbutton); okbutton.addactionlistener(this); settitle("new bank account creation"); setvisible(true); setlocationrelativeto(null); setsize(270, 300); setdefaultcloseoperation(exit_on_close); setlayout(new flowlayout()); } @override public void actionperformed(actionevent e) { char[] pass = enterpassword.getpassword(); string passstring = new string(pass); char[] passconfirm = enterconfirmpassword.getpassword(); string passstringconfirm = new string(passconfirm); username = enterusername.gettext(); if (e.getsource() == okbutton) { if(username == null || username.isempty() || passstring == null || passstring.isempty() || !passstring.equals(passstringconfirm)) { enterusername.settext(""); enterpassword.settext(""); enterconfirmpassword.settext(""); border redline = borderfactory.createlineborder(color.red); enterusername.setborder(redline); enterpassword.setborder(redline); enterconfirmpassword.setborder(redline); repaint(); } } super.dispose(); int response = 0; string firstdesposit = joptionpane.showinputdialog("welcome " + username + ". enter initial deposit."); initialdeposit = double.parsedouble(firstdesposit); if (response == joptionpane.ok_option) { new menu(); } } }
your if
test cannot correct. if of string
(s) null
nullpointerexception
. think wanted
if (username == null || username.isempty() || passstring == null || passstring.isempty() || !passstring.equals(passstringconfirm)) { enterusername.settext(""); enterpassword.settext(""); enterconfirmpassword.settext(""); }
then ui code should check if empty before allowing user proceed. finally, in code above believe might use setborder()
give fields red border.
if (username == null || username.isempty() || passstring == null || passstring.isempty() || !passstring.equals(passstringconfirm)) { border redline = borderfactory.createlineborder(color.red); enterusername.settext(""); enterpassword.settext(""); enterconfirmpassword.settext(""); enterusername.setborder(redline); enterpassword.setborder(redline); enterconfirmpassword.setborder(redline); }
edit
based on code provided, need in else!
if(username == null || username.isempty() || passstring == null || passstring.isempty() || !passstring.equals(passstringconfirm)) { enterusername.settext(""); enterpassword.settext(""); enterconfirmpassword.settext(""); border redline = borderfactory.createlineborder(color.red); enterusername.setborder(redline); enterpassword.setborder(redline); enterconfirmpassword.setborder(redline); repaint(); } else { // <-- add super.dispose(); int response = 0; string firstdesposit = joptionpane.showinputdialog( "welcome " + username + ". enter initial deposit."); initialdeposit = double.parsedouble(firstdesposit); if (response == joptionpane.ok_option) { new menu(); } }
Comments
Post a Comment