I am trying to create a program that asks for a password. I created two separate programs that I thought would yield the desired results; however in both programs when the password is entered it says "access denied" . Below are the two programs.
PROGRAM 1
import java.util.Scanner;
public class program1{
public static void main (String args[])
{
System.out.println("Hello User");
String password;
password = (String) ("qwer");
System.out.println("password is " + password); // for troubleshooting
Scanner keyboard = new Scanner(System.in);
String guess;
System.out.println("Please Enter the Password");
guess = keyboard.next();
if (guess == password)
System.out.println ("Access Granted");
else if (guess != password)
System.out.println("Access Denied");
}
}
PROGRAM 2
import java.io.*;
public class program2{
public static void main (String args[])
{
InputStreamReader istream = new InputStreamReader(System.in);
BufferedReader bufRead = new BufferedReader(istream);
System.out.println("Hello User");
String password;
password = (String) ("qwer");
System.out.println ("password is " + password);
try{
System.out.println("Please enter password");
String guess = bufRead.readLine();
if (guess == password)
System.out.println ("Access Granted");
else if (guess != password)
System.out.println("Access Denied");
}
catch (IOException err){
System.out.println("Error reading line");
}
}
}
I am sure it is probably some stupid mistake as I am relatively new to java. Any help would be greatly appreciated.
Thank you,
Jared McLean