Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Attempting to create a password prompt program

  1. #1
    Junior Member
    Join Date
    Mar 2013
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Attempting to create a password prompt program

    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


  2. #2
    Member
    Join Date
    Feb 2013
    Posts
    45
    Thanks
    0
    Thanked 5 Times in 5 Posts

    Default Re: Attempting to create a password prompt program

    you have made mistake on if condition that's is like the one string is compare to another in java we use equals() method instead of ==.

    So you create the condition like that...
    if (guess.equals(password)){
    //your code here
    }
    else if (!guess.equals(password))
    //your code here
    }
    Regards
    Android developer
    Trinay Technology Solutions
    http://www.trinaytech.com
    5705750475

  3. The Following User Says Thank You to Tamilarasi For This Useful Post:

    JaredMcLean (March 13th, 2013)

  4. #3
    Junior Member
    Join Date
    Mar 2013
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Attempting to create a password prompt program

    Thank you very much for your reply! I tried the program with guess.equals(password) and it worked.

Similar Threads

  1. Attempting to create array for Color Class - invalid integer inputs
    By JavaClass in forum What's Wrong With My Code?
    Replies: 4
    Last Post: February 27th, 2013, 07:07 PM
  2. Can Someone please help me with my password program?
    By mkrage in forum What's Wrong With My Code?
    Replies: 6
    Last Post: October 17th, 2012, 11:16 AM
  3. Java program not running in command prompt
    By Padmahasa in forum Computer Support
    Replies: 1
    Last Post: August 10th, 2012, 07:22 AM
  4. How to create a password protected excel sheet....
    By ArunSRPM in forum Member Introductions
    Replies: 1
    Last Post: April 18th, 2012, 01:25 PM
  5. Program is attempting to change booleans yet the result is unsuccesful.
    By CoolGuy134 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: September 26th, 2011, 08:48 AM