Reputation: 79
So I made this really hard to follow method a while ago and I came back to add in another indented if statement to check against an array list of Users using a for loop. Before I added this in, the method worked fine but now I am getting an error on the last 'else' keyword. It says "syntax error. please delete this token". I have no errors when I delete the last else keyword but I'm not sure why when they are four if keywords - should there not be a fourth else keyword? Thanks for any help given.
//method to validate input by user to log in
public void validateInput() {
//presence check on username
if (qi.getEnteredName().length() > 0) {
//compare entered username to stored user accounts
for(int i = 0; i < users.size(); i++) {
if (qi.getEnteredName().equalsIgnoreCase(getUserList().get(i).getUsername())) {
//presence check on password
if (qi.getEnteredPass().length > 0) {
//ensures password is at least 6 char long
if(qi.getEnteredPass().length > 5) {
qi.getMainCards().next(qi.getPanels()); //getPanels() == cardPanel
} else {
JOptionPane.showMessageDialog(null,
"Your password must be at least six characters long.",
"Password Violation", JOptionPane.WARNING_MESSAGE);
}
}else {
JOptionPane.showMessageDialog(null,
"Your did not enter a password.",
"Password Violation", JOptionPane.WARNING_MESSAGE);
}
}
else { JOptionPane.showMessageDialog(null,
"You are not registered.",
"User Violation", JOptionPane.WARNING_MESSAGE);
}
else {
JOptionPane.showMessageDialog(null,
"You did not enter a username. Please try again.",
"Username Violation", JOptionPane.WARNING_MESSAGE);
}
}
}
}
Upvotes: 1
Views: 2549
Reputation: 1016
Well, basically your last two else
statements are matching up with the same if
statement which is probably not what you want. You'd have to move the second else statement outside of another }
. You have this:
else { JOptionPane.showMessageDialog(null,
"You are not registered.",
"User Violation", JOptionPane.WARNING_MESSAGE);
}
else {
JOptionPane.showMessageDialog(null,
"You did not enter a username. Please try again.",
"Username Violation", JOptionPane.WARNING_MESSAGE);
}
}
But you really want this:
else { JOptionPane.showMessageDialog(null,
"You are not registered.",
"User Violation", JOptionPane.WARNING_MESSAGE);
}
}else {
JOptionPane.showMessageDialog(null,
"You did not enter a username. Please try again.",
"Username Violation", JOptionPane.WARNING_MESSAGE);
}
This is why it's very important to format your code correctly and neatly with proper indentation. In Eclipse (if you're using that) you can select everything and hit ctrl + i (I think) or cmnd + i depending on whether you're using a PC or a mac, and it will properly format your indentation.
That being said, it's probably a lot easier to do if
, else if
, and else
statements than what you're doing here.
Upvotes: 4
Reputation: 7998
You missed the right place in your if statement, as already mentioned by other answers. To prevent such annoying errors I would recommend to organize the code in more readable manner. For your case it could be something similar to (if I got the logic correctly):
public void validateInput() {
final String name = qi.getEnteredName();
final String password = qi.getEnteredPass();
if (empty(name)) {
showWarn("Username Violation", "You did not enter a username. Please try again.");
} else if (!registered(name)) {
showWarn("Username Violation", "You are not registered.");
} else if (empty(password)) {
showWarn("Password Violation", "Your did not enter a password.");
} else if (password.length < 6) {
showWarn("Password Violation", "Your password must be at least six characters long.");
} else {
// we are good
qi.getMainCards().next(qi.getPanels());
}
}
private boolean registered(String name) {
for(int i = 0; i < users.size(); i++) {
if (name.equalsIgnoreCase(getUserList().get(i).getUsername())) {
return true;
}
}
return false;
}
private void showWarn(String title, String message) {
JOptionPane.showMessageDialog(null,
message,
title, JOptionPane.WARNING_MESSAGE);
}
private boolean empty(String string) {
return string != null && !string.isEmpty();
}
Upvotes: 0
Reputation: 3543
You have two else
statements.
On the top level you have if
, else
and another else
.
It should be if
, else if
, and the ultimate condition would be else
. If you are dealing with many if
statements, every next if
should be else if
, because else
means "if everything else is not true".
Upvotes: 2