验证电子邮件地址后如何进行while循环?

时间:2015-11-08 01:51:02

标签: java do-while

// My Scanner
Scanner input = new Scanner(System.in);   
//using Do While Loop
do {
    //Asking user to enter email
    System.out.println("enter your email:");
    //Read and safe input in to Var userEmail
    String userEmail = input.next();
    //Check for contains '@' and '.com' simbols
    Pattern pattern = Pattern.compile("\\S+?@\\S+?\\.com");
    //And it checking in users entered email
    Matcher matcher = pattern.matcher(userEmail);
    //if userEmail contain '@'and '.com' print next line
    if (matcher.matches()) {
        System.out.println("Matches"); // Prints this for this email    
    }
    //if user put email with out '@'and'.com' print next line
    else {
        System.out.println("your email should 
        looks like this sample bob.Dillon@gmail.com");
    }
// And here I have a problem don't know what to type in
// so that it starts looping until user input will be 100% correct.
} while(!matcher.matches());

有人可以帮助您在此处while(here);做些什么来使其循环播放?

1 个答案:

答案 0 :(得分:1)

您想查看用户是否在这些字段中输入了任何内容。所以,检查一下:

if (INPUTVALUE.length > 0) { //THEY ENTERED SOMETHING
    // do something
}

然后,将其放入while声明中。像这样:

// My Scanner
 Scanner input = new Scanner(System.in);   
    //using Do While Loop
    do{
        //Asking user to enter email
        System.out.println("enter your email:");
        //Read and safe input in to Var userEmail
        String userEmail = input.next();
        //Check for contains '@' and '.com' simbols
        Pattern pattern = Pattern.compile("\\S+?@\\S+?\\.com");
        //And it checking in users entered email
        Matcher matcher = pattern.matcher(userEmail);
        //if userEmail contain '@'and '.com' print next line
        if (matcher.matches()) {
            System.out.println("Matches"); // Prints this for this email    
        }
        //if user put email with out '@'and'.com' print next line
        else{
            System.out.println("your email should 
            looks like this sample bob.Dillon@gmail.com");
        }
    //And here I have a problem don't know what to type in so that it starts looping until user input will be 100% correct 
    }while(INPUTVALUE.length > 0);

您需要:

}while(INPUTVALUE.length > 0);

打破循环

只需删除用户在执行结尾处输入的所有值。那样,INPUTVALUE.length<这将打破循环!祝你好运!