-1

I am a beginner in java and I want to use a centinel loop to repeat code as many times as the condition is not met. However I can't get it to work. This is my code so far:

import java.io.*;

public class Test
{
    public static void main(String args[]) throws IOException
    {
        InputStreamReader isr = new InputStreamReader(System.in);
        BufferedReader br = new BufferedReader(isr);

        String password = "Pass";
        System.out.print("Enter the password: ");
        String input = br.readLine();

        while(input != password)
        {
            System.out.println("Wrong password.\n");
            System.out.print("Enter the password: ");
            input = br.readLine();
        }
    }
}

However, every time I put the password it simply doesn't work. Could anybody help me please?

khelwood
  • 55,782
  • 14
  • 81
  • 108

1 Answers1

0

You cannot use == (or !=) to check string equality. Checking string equality is done with the string equals method which compares the strings contents rather than their reference equality (like ==).

!input.equals(password)

PS: typically that type of loop is referred to as a while loop. But if you want to use your current name I'm pretty sure it would be spelled "Sentinel".

TristanKJ
  • 33
  • 1
  • 7