-2

I have written the below code but i am unable to make it loop if the input entered is false.Kindly help me.

System.out.println("Please enter your 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 is " +emailaddress);
Bruce_Wayne
  • 1,564
  • 3
  • 18
  • 41
Vdev
  • 101
  • 1
  • 5
  • 17

3 Answers3

0
   boolean b;
   do {
        System.out.println("Please enter your 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;
        b = testString.matches(email_regex);
        System.out.println("String: " + testString + " :Valid = " + b);
        System.out.println("Email address is " +emailaddress);
    }while(!b);
Bruce_Wayne
  • 1,564
  • 3
  • 18
  • 41
  • Hi its going back to the loop even if correct input is given...:( – Vdev Nov 27 '14 at 15:50
  • check the email u have entered, loop will break as soon as 'b' contains 'true' value, CAREFULLY re-examine the code and the email u are using for testing. – Bruce_Wayne Nov 27 '14 at 15:55
  • yes loop is breaking once true value is entered but instead of coming out of the loop its rather displaying "please enter your email address" again – Vdev Nov 27 '14 at 16:01
0
    String testString;
    String emailaddress;
    boolean b = false;

    do {
        System.out.println("Please enter your email address ex:xyz@gmail.com");
        Scanner name = new Scanner(System.in);
        emailaddress = name.nextLine();

        String email_regex = "^[_A-Za-z0-9-\\+]+(\\.[_A-Za-z0-9-]+)*@"
    + "[A-Za-z0-9-]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$";
        testString = emailaddress;
        b = testString.matches(email_regex);
        System.out.println("String: " + testString + " :Valid = " + b);
    } while (!b);

    System.out.println("Email address is " + emailaddress);
Salih Erikci
  • 5,076
  • 12
  • 39
  • 69
  • Boolean b needs to be out of do while – Sikorski Nov 27 '14 at 15:28
  • i am getting in 2 lines Boolean b = testString.matches(email_regex); System.out.println("Email address is "+testString);--one is (b is a duplicate local varaible and the other is teststring canot be resolved as a varaibe...kindly help – Vdev Nov 27 '14 at 15:36
  • @NandithaVasudev Edit your code as i show in my answer. – Salih Erikci Nov 27 '14 at 16:13
  • i have edited it displays this way even though i have entered the correct email Please enter your email address ex:xyz@gmail.com nanditha22@gmail.com String: nanditha22@gmail.com :Valid = false Please enter your email address ex:xyz@gmail.com – Vdev Nov 27 '14 at 16:25
  • Your regex is wrong.I used the regex from http://www.mkyong.com/regular-expressions/how-to-validate-email-address-with-regular-expression/ and it worked fine. See my edited answer with this regex. – Salih Erikci Nov 27 '14 at 16:31
0

Here goes the 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 your email address ex:xyz@gmail.com");
        String emailaddress=name.nextLine();
        boolean a = checkEmailvalidity(emailaddress);
       if(a){
          System.out.println("Valid email");
        } else {
          System.out.println("InValid email");
           inputEmail();
       }
}

}

here is with your 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 your first name:");     
             while(!name.hasNext("[a-zA-Z]+")){ 
                 System.out.println("Please re-enter your name, use alphabets)
                  System.out.println("Please enter your first name:"); 
                   name.nextLine(); 

              } 
        String firstname=name.nextLine(); 
        System.out.println("Your firstname is " + firstname); 
       inputEmail();
We are Borg
  • 5,117
  • 17
  • 102
  • 225