-1

I'm trying to use a while loop for email validation in JFrame (An email has to have "@" and "com" in it). So far I have:

while(!emailInput.getText().matches(".*[@com].*")) {
if (emailInput.getText().matches(".*[@com].*")) {
break;
} //from if
}
if((!emailInput.getText().matches(".*[@com].*"))) {
JOptionPane.showMessageDialog(null, "Your email does not seem to be valid. It should be in the form of myemail@myemail.com. Please try again!");
}

Of course, the while run will be an infinite one - once I tested the program by entering a random email (say, "test"), the condition will always be false. I dare not to put show the dialog (saying that the email doesn't seem right) in the while loop because it will also show the message infinite number of times.

But that is a little bit counterintuitive - since I want the user to keep entering the email until he gets it right (break in while loop).

I have tried to use only if statement but it seems that my if code only works once - so I'm counting on while loop. However, is there any way to fix this, even without using while loop? I am using this for password validation and I am having trouble as well. I am open to any suggestion.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
DiMario
  • 33
  • 1
  • 8
  • 1
    Why don't you validate the email at the press of a button ? Why do you need a `while` loop ? – user1234SI. Mar 07 '20 at 19:27
  • My code is actually in the ActionListener of my "REGISTER" button. :( – DiMario Mar 07 '20 at 19:29
  • Oh wait, I think that I understand your point. I could have gotten rid of the while loop because if the user clicks on it, it would have check the validation so an if statement would have been sufficient. And he will keep entering the email and clicking the register button until he gets it, is that correct? – DiMario Mar 07 '20 at 19:30
  • All right then. And why do you need the while loop anyway ? – user1234SI. Mar 07 '20 at 19:30
  • Yep, that was my question. – user1234SI. Mar 07 '20 at 19:31
  • That answers my question instantly - I'm dumb as soup haha! So now I can get rid of the while loop and just use an easy if statement then. Thank you! – DiMario Mar 07 '20 at 19:32

1 Answers1

2

As Sebastian suggested, the solution is to use a button, get rid of the while loop, and just use an if statement. The reason is that if the user clicks on it, the program would have checked the validation - and it checks the validation whenever the user clicks on the button. The process ends when the user enters a valid email and clicks on the button for the final time.

DiMario
  • 33
  • 1
  • 8
  • 1
    This not related to your question about `while` loops - but bear in mind that your regex `.*[@com].*` does not work the way you think it works. It will allow many invalid formats for email addresses. You should use a tester - for example [here](https://www.freeformatter.com/java-regex-tester.html#ad-output). Also, bear in mind there has been a lot of discussion on what is the correct way to validate email addresses - for example see [this question and its answers](https://stackoverflow.com/questions/201323/how-to-validate-an-email-address-using-a-regular-expression/21595782). – andrewJames Mar 07 '20 at 20:07
  • Thank you so much! I have done some research and fixed it! – DiMario Mar 07 '20 at 22:47