Take a moment to look over your code...
String name = "";
while (!name.equals("xma")) {
name = JOptionPane.showInputDialog(null, "Whats your name?");
JOptionPane.showMessageDialog(null, "You are not allowed to pass.");
}
So your code is basically saying, while name is not equal to "xma", show error message, then prompt for name
So, in order to get this to work (in it's current state), after you've got the input from the user, you'd need to test the name value and display the error message if it's not correct and check it again when testing if your should exit the loop.
while (!name.equals("xma")) {
name = JOptionPane.showInputDialog(null, "Whats your name?");
if (!"xma".equals(name)) {
JOptionPane.showMessageDialog(null, "You are not allowed to pass.");
}
}
This is some what inefficient, as you need two places to check the condition. Very easy to screw that up.
Arguably, you could use a state flag instead, for example...
boolean isCorrect = false;
while (!isCorrect) {
name = JOptionPane.showInputDialog(null, "Whats your name?");
isCorrect = "xma".equals(name);
if (!isCorrect) {
JOptionPane.showMessageDialog(null, "You are not allowed to pass.");
}
}
Now, we're only checking the condition once and then responding to the flag.
But, we could do something a little bit different. The fact is, you have to enter the loop at least once anyway, so a while-do loop is necessarily the right approach (it works, don't get me wrong). Instead, I'd approach the problem from a do-while point of view.
This basically puts the conditional check at the end of the loop, instead of the start...
String name = null;
do {
if (name != null) {
JOptionPane.showMessageDialog(null, "You are not allowed to pass.");
}
name = JOptionPane.showInputDialog(null, "Whats your name?");
} while (!"xma".equals(name));
JOptionPane.showMessageDialog(null, "Hello Mr." + name + "!");
Now, this is simular to the "state flag" idea presented above, except, we're using the name as the flag instead. It's assumed that if name is not null and we've gone back to the start of look, then name is incorrect (because otherwise we'd have exited the loop).
A a side effect, see what happens if you press the "cancel" button ;)
Last but not least, we could merge the two ideas. That is, we use a state flag and a do-while loop. This workflow might make it a bit easier to understand, as it flows in a natural way, that, you ask for the name, validate it and then display the error message...
boolean isCorrect = false;
String name = null;
do {
name = JOptionPane.showInputDialog(null, "Whats your name?");
isCorrect = "xma".equals(name);
if (isCorrect) {
JOptionPane.showMessageDialog(null, "You are not allowed to pass.");
}
} while (!isCorrect);
JOptionPane.showMessageDialog(null, "Hello Mr." + name + "!");
This highlights the fact, that there is a number of ways to skin this solution ;)
Scanneris (usually) for reading text input from the console (System.in). It is entirely separate fromJOptionPane(Swing). Aside from wastingSystem.in, this looks like it should work; if it doesn't you need to explain in more detail what the behavior is.