While loop for email validation in Java JFrame - java

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.

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.

Related

Java loop gets stuck and doesnt exit

So im creating a game of blackjack as part of an assignment for my college. I have to create some kind of method to allow the player to Hit or Fold.I decided to write this part of code which checks the input:
do {
System.out.println(playernames[j]+" (H/F)");
playerinput=input.next();
} while (!((playerinput.equals("H"))||(playerinput.equals("F"))));
Everything runs smoothly until it reaches this loop.The workspace im working(eclipse) doesnt show any syntax problems nor any java exceptions occur.I tried editing this to find what is happening by using a temp boolean set to false before the loop and after the loop it gets set to true.The output didnt change meaning it didnt get past the loop.
This code works, but only if the user types a capital H or F. Maybe that's the problem? If you want to allow both lower case and upper case letters, you can use playerinput.equalsIgnoreCase("H").
Also, input.next() will only return after the user has pressed enter. Just pressing the H or F key is not enough.

Eclipse 4 RCP Disable Part Switch

In my (pure) E4 App I want to force the User to enter something in a Text Field before he can move on.
Currently, if nothing has been entered into the Text Field and the focusLost Event is triggered, I reset focus to the Text Field.
Via a ModifyListener I check, if the entered String equals "" and if yes, a fake tooltip is displayed, telling the User to enter something into the Text Field.
The Problem is, if I have two Parts on a PartStack and the Text Field is on my first Part, the User is still able to trigger a Part switch and work on the other Part without having to enter something into the Text Field on the first Part first.
How is it possible to prohibit the User from switching between these Parts, as long as nothing is entered in the Text Field on the first Part?
I donĀ“t want to hide Part 2, the App should still look the same, the user should just not be able to do anything until something has been entered into the Text Field.
If you don't want to hide Part 2, then disable the widgets in that part instead. And display a message that asks the user to provide the necessary input back in Part 1. If the user does, hide that message and enable your widgets.
For example:
if (!isInputSatisfactory())
{
displayMessage("Invalid input");
myButton.setEnabled(false);
}
else
myButton.setEnabled(true);

ParkingSimulation infinite while loop crisis

I'm a beginning computer science student and we've been asked to complete a project that animates random generated car objects moving to randomly generated parking spots on a city map grid. I've developed a GUI for the buttons, text fields, and text areas. Everything works as required except for at the end of the animation, when all cars have reached parking spots, I need to display analytics in my JTextArea. My buttons are set up properly and I will show you where the code takes place below:
else if (e.getSource() == start) {
setAnimate(true);
if(simulator.simulationFinished()) {
createAnalytics();
}
}
So here I have implemented an action listener on the button "start" that begins the animation. Currently it:
currently it begins the animation and all the cars travel to the parking spots as intended.
it displays analytics that are derived from before the animation began (Analytics include: car ID, number of moves, average number of spots tried, average distance travelled)
then the animation will conclude.
If I press the start button again it will display the proper analytics.
I know that in order for the program to display the right analytics the moment the program finishes I most likely need a while loop, however I haven't been able to dream anything up that will not create an infinite loop and require me to manually terminate the program via console.
while(!simulator.simulationFinished()) {
if(simulator.simulationFinished() == true)break; {
createAnalytics();
}
}
I've also tried this among several hundred other variations of all the loops in existence. Following from my logic, I need the while loop so that it will keep checking to see if the simulation is finished so that I can execute my method that generates analytics, but if don't give the while loop something to do it just goes on forever and crashes. I'm at a loss, any help would be appreciated. Thanks.
First: no, you don't need a while loop necessarily. You could use the observer pattern instead (for example) and your object would be notified as soon as the simulation had finished. As to your question: the if inside the while is obviously superfluous (as is the == true). The real problem seems to be that simulator.simulationFinished() never returns true. Could you post the code of that function and the code (and any code that directly influences the return value of it)?
Without going into details, there's a serious problem with your while loop.
See, it loops as long as simulator.simulationFinished() returns false.
However, within the loop, you check for the opposite - which will never happen.
I would recommend using listeners of some sort, although the requirement is not clear enough for me to advise any further.

How to program showDialogMessage?

I just programmed an interface about user registration. For delete user part, when i click delete button, the program will show a box asking "are you sure about deleting the user?" If i choose yes, then how can i program to show a showDialogMessage to tell the user the user has been deleted?
Just pitching here, but if you are using swing, look into JOptionPane especially the showMessageDialog methods.
JOptionPane.showConfirmDialog(Component parentComponent, Object message) This is for getting confirmation from user.
JOptionPane.showMessageDialog(Component parentComponent, Object message) this is for showing that the user has been deleted.
Since your question is incomplete, answering in the contrast of Swing
int i=JOptionPane.showConfirmDialog(null,"Are you sure about deleting the user?");
if(i==0) JOptionPane.showMessageDialog(null,"User deleted");
Confirmation box return 0 if chosen is yes, 1 if chosen is no and 2 if chosen is cancel
Go through JOptionPane you will get better idea.

Creating a pop up box in Java

I want to create a pop up box that is activated when I click a "Check" button. What it does is ask the user if they are sure about what they requested. If yes, it does the request. If no, it goes back to it's normal state.
I know what I said is a bit ambiguous but I want to create various types of pop up boxes.
So I was wondering is there a website that has generic pop up boxes with the code given?
I just need a simple code which I can expand on.
I think JOptionPane is what you want.
showConfirmDialog: Asks a confirming
question, like yes/no/cancel.
showInputDialog: Prompt for some
input.
showMessageDialog: Tell the
user about something that has
happened.
showOptionDialog: The Grand
Unification of the above three.
A small example to get a Yes-No popup like you asked for would be:
if (JOptionPane.showConfirmDialog(null, "Are you sure about your request?", "Request",
JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE)
== JOptionPane.YES_OPTION)
{
//Do the request
}
else
{
//Go back to normal
}
This solution, however, only works if you are using Swing.
You shoudl check JDialog to create your custom message dialogs or you can use standard message dialogs in JOptionPane.

Categories

Resources