2015-11-08 56 views
0
// 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("\\[email protected]\\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 [email protected]"); 
    } 
// 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

那么,呃,所有'Blockquote's是什么意思? – Ross

+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("\\[email protected]\\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 [email protected]"); 
     } 
    //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 < 0.这将打破循环!祝你好运 !

+0

任何想法如何停止此循环:) –

+0

@KonstantinF只是擦除用户输入的所有值在**做**的结束**这样,INPUTVALUE.length < 0.打破循环!祝你好运 ! –

相关问题