Inputting and validating the email address in JAVA -
i have written below code unable make loop if input entered false.kindly me.
system.out.println("please enter email address ex:xyz@gmail.com"); string emailaddress=name.nextline(); string email_regex = "[a-z]+[a-za-z_]+@\b([a-za-z]+.){2}\b?.[a-za-z]+"; string teststring = emailaddress; boolean b = teststring.matches(email_regex); system.out.println("string: " + teststring + " :valid = " + b); system.out.println("email address " +emailaddress);
here goes 3 functions :
public class abc{ public static void main(string[] args){ inputemail(); } public boolean checkemailvalidity(string emailaddress){ string email_regex = "[a-z]+[a-za-z_]+@\b([a-za-z]+.){2}\b?.[a-za-z]+"; boolean b = teststring.matches(email_regex); return b; } public void inputemail(){ system.out.println("please enter email address ex:xyz@gmail.com"); string emailaddress=name.nextline(); boolean = checkemailvalidity(emailaddress); if(a){ system.out.println("valid email"); } else { system.out.println("invalid email"); inputemail(); } }
}
here updated answer :
package smsmain; import java.util.scanner; public class cstudentinfo { public static void createstudent() { scanner name = new scanner(system.in); system.out.println("please enter first name:"); while(!name.hasnext("[a-za-z]+")){ system.out.println("please re-enter name, use alphabets) system.out.println("please enter first name:"); name.nextline(); } string firstname=name.nextline(); system.out.println("your firstname " + firstname); inputemail();
Comments
Post a Comment