I have an \'if-else\' statement that\'s throwing 13 \"illegal start of type\" \"
ID: 3624385 • Letter: I
Question
I have an 'if-else' statement that's throwing 13 "illegal start of type" "; expected" and "<identifier> expected"....no idea what's going on.See code below. (Only the if-else not the whole program.
if(username.equals("") || password.equals(""))
{
loginButton.setEnabled(false);
JLabel errorFields = new JLabel("<html><FONT COLOR = RED>You must enter a username and password to login.</FONT></html>");
JOptionPane.showMessageDialog(null,errorFields);
id.setText("");
pass.setText("");
loginButton.setEnabled(true);
this.setVisible(true);
}
else
{
JLabel optionLabel = new JLabel("<html><FONT COLOR = BLUE>You entered</FONT><FONT COLOR = RED> <B>"+username+"</B></FONT> <FONT COLOR = BLUE>as your username.<BR> Is this correct?</FONT></html>");
int confirm = JOptionPane.showConfirmDialog(null,optionLabel);
switch(confirm)
{
case JOptionPane.YES_OPTION: // Attempt to Login user
loginButton.setEnabled(false); // Set button enable to false to prevent 2 login attempts
TextFieldHandler check = new TextFieldHandler();
id.addActionListener(check);
pass.addActionListener(check);
check.actionPerformed();
this.setVisible(false);
break;
case JOptionPane.NO_OPTION: // No Case.(Go back. Set text to 0)
loginButton.setEnabled(false);
id.setText("");
pass.setText("");
loginButton.setEnabled(true);
break;
case JOptionPane.CANCEL_OPTION: // Cancel Case.(Go back. Set text to 0)
loginButton.setEnabled(false);
id.setText("");
pass.setText("");
loginButton.setEnabled(true);
break;
}
}
Explanation / Answer
The likely reason for this error is that your if-else statement is not properly contained within a method.
Check to be sure that it is contained within a method that is inside the class body. Java expects an identifier for the method (a header, such as "public void methodName()")
(From inspection of the code that you posted, there seems to be no errors in what I can see, but check what is around it to ensure that it is inside a method and not freely floating inside a class body.)
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.