import javax.swing.*;
public class Password {
public static void main(String [] args)
{
String password;
do
{
password=JOptionPane.showInputDialog("Password, Please");
if(password==("N123U"));
JOptionPane.showMessageDialog(null,"Welcome");
else
JOptionPane.showMessageDialog(null,"Try again");
}while(password.equals("N123U")==false);
}
}

- 20,287
- 13
- 71
- 105

- 1
- 1
- 1
-
Dennis, while you're probably right, it's likely that the poster did not assume the issue was related to string comparison (and in fact it isn't fully related to that). – Giulio Franco Nov 02 '13 at 00:54
6 Answers
You have a semicolon at the end of your if
statement. Having that ends the if
statement. Remove it, if statements should not have a semicolon.

- 31,086
- 2
- 52
- 86
The other answers are correct, you have a ;
at the end of your if
statement, but so you can see...
if(password==("N123U"));
^--- This is your problem...
This is one of the reasons why it is recommend to always use braces around your statements, even if they are a single line...
if(password==("N123U")) {
JOptionPane.showMessageDialog(null,"Welcome");
} else {
JOptionPane.showMessageDialog(null,"Try again");
}
The other problem you are (going) to have is the fact that you are using ==
to compare String
s, which is only comparing the object references, which is unlikely to be equal. Instead you should be using String#equals
, for example...
if("N123U".equals(password)) {...}
Which will also help you avoid NullPointerException
s

- 343,457
- 22
- 230
- 366
Semicolon after if
signifies end of statement: the next line is not considered part of the if clause. Remove the semicolon to get the right flow. In other words,
if(password==("N123U"));
Should be
if(password==("N123U")) {
// do something
}
else {
// do something else
}
In principle when each of the if
and else
are single statements you can omit the curly braces, but that is usually asking for trouble at some point in your future...

- 45,857
- 6
- 70
- 122
if(password==("N123U"));
Two thing wrong. Shouold be
if("N123U".equals(password))
// don't compare strings with == and you have a semicolon, ending the statement

- 205,037
- 37
- 486
- 720
I guess, there are two errors: you try to compare a string with "==" in Java - always use .equals for Strings, like you do in the "while" part. The "if" part is terminated by the semicolon - let me try to correct that code:
import javax.swing.*;
public class Password {
public static void main(String [] args)
{
String password;
do
{
password=JOptionPane.showInputDialog("Password, Please");
if(password.equals("N123U")) {
JOptionPane.showMessageDialog(null,"Welcome");
} else {
JOptionPane.showMessageDialog(null,"Try again");
}
}while(password.equals("N123U")==false);
}
}

- 469
- 1
- 6
- 14
Actually, if you'd just put a block around the "then" statement (even if it's just that one line), the error would be more obvious (visually that is).
For future reference: a single ; is an empty (aka "do nothing") statement. So what you tell your programm to do is (pseudo-code):
if (something)
<do nothing>
JOptionPane.doSomething()
else // <- syntax error here, we're not in an if anymore, so this is invalid
...
...

- 5,875
- 1
- 20
- 40