// 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);
以使其循环吗?
最佳答案
您想查看用户是否在这些字段中输入了任何内容。因此,请像这样检查:
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);
打破循环:
只需删除用户在 do 末尾输入的所有值即可。这样,INPUTVALUE.length < 0。这将打破循环!祝你好运!
关于java - 验证电子邮件地址后如何进行 while 循环?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33589876/