0

I am coding a little program that prompts the user to enter in a password, if the user enters the incorrect password, then the program will keep prompting the user to enter in the password. Once the user enters in the correct password, then the program exits. I created a do-while loop to keep looping until the correct password is entered, but when the correct password is entered, the program continues to loop and does not exit. The variable value does not seem to reach the while clause of the program. Can someone please show me where I am going wrong? My code is as follows:

    import java.util.*;
    import javax.swing.*;

    public class do_whilePassword
    {

public static void main ( String [] args )
{
   String choice = "";

     do
        {
            choice = JOptionPane.showInputDialog("Please enter in a password");

        }while(choice != "John" );

}
}
user3468512
  • 53
  • 1
  • 11

1 Answers1

0

Just its related to comparing Java Strings using .equal NOT =

do
{
    ...

}while(! choice.equals("John"));
Ahmed Nabil
  • 17,392
  • 11
  • 61
  • 88
  • But the thing is, using String.equals is going to make it keep looping. I only want it to loop when the password is NOT equal to John, so that the loop ends when the password is equal to John. I also noticed, the while part of the loop is not getting the value of the choice variable, it comes out blank., and I think that is why it is not working. – user3468512 Dec 27 '16 at 18:24