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 23 of 23

Thread: Need help with programming a simple password checker.

  1. #1
    Junior Member
    Join Date
    Aug 2014
    Posts
    24
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Need help with programming a simple password checker.

    Use Java to write and run a simple console-window program for checking passwords. The program must satisfy the following requirements:
    · When the program runs, it should first print its name, e.g., Password Checker
    · The program should next prompt the user to enter a username, e.g.,
    Please enter your username:
    · The program should next prompt the user to enter a password, e.g.,
    Please enter your password:
    · If the user enters a correct password, i.e., one that matches an internal secret password, the program responds
    You are approved by access control!, and quits.
    · If the user enters an incorrect password, the program responds
    Try again:
    · If the user enters a correct password matching the secret one, the program responds
    You are approved by access control!, and quits.
    · If the user enters an incorrect password, the program responds
    Sorry, and quits.

    I kinda know where to begin but I have no idea with most of this assignment so any help would be awesome! thank you in advanced

    this is all i have:

    package class1;
    import java.util.Scanner;
    public class class1 {

    public static void main(String[] args) {
    // TODO Auto-generated method stub

    System.out.println("Password Checker");
    System.out.println("Please enter your username");

    Scanner keyboard = new Scanner(System.in);

    int username = keyboard.nextInt () ;


    }

    }
    Last edited by HappyCamper; August 30th, 2014 at 01:58 AM.


  2. #2
    Junior Member
    Join Date
    Apr 2014
    Posts
    7
    My Mood
    Inspired
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Since you kinda know where to begin... Do that. Your best bet when it comes to getting help is to try until your completely baffled and then post what you've got. From there someone will help push you in the right direction. Hint: Google java Scanner and java equals method.

  3. #3
    Junior Member
    Join Date
    Aug 2014
    Posts
    24
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Need help with programming a simple password checker.

    okay i updated my post

  4. #4
    Junior Member
    Join Date
    Apr 2014
    Posts
    7
    My Mood
    Inspired
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Assuming the password is the same for "all users", you need to establish what that password is in a variable. Then do the same thing you did for username. Then compare them. You will also need a loop to cycle through password entry/check twice.

  5. #5
    Junior Member
    Join Date
    Apr 2014
    Posts
    7
    My Mood
    Inspired
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    I wouldn't use ints unless that's what the assignment calls for. You will have to do some extra code for that.

  6. #6
    Junior Member
    Join Date
    Aug 2014
    Posts
    24
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Need help with programming a simple password checker.

    i am very new at this haha youre going to have to go over this with me..i mainly am using ints because that is what we have learned so far haha

  7. #7
    Junior Member
    Join Date
    Apr 2014
    Posts
    7
    My Mood
    Inspired
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Are you familiar with a String?

  8. #8
    Junior Member
    Join Date
    Apr 2014
    Posts
    7
    My Mood
    Inspired
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Keeping it the way it is... You still need to do what I recommended in post #4. You need an int variable to hold the correct password for starters.

  9. #9
    Junior Member
    Join Date
    Aug 2014
    Posts
    24
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Need help with programming a simple password checker.

    How do I go from typing in the username to then typing in the password because every time i type in the username it gives me an error

  10. #10
    Member Ada Lovelace's Avatar
    Join Date
    May 2014
    Location
    South England UK
    Posts
    414
    My Mood
    Angelic
    Thanks
    27
    Thanked 61 Times in 55 Posts

    Default Re: Need help with programming a simple password checker.

    Your password input should be directly below/after your user name input. Do not start
    checking for results until the program has both viable pieces of data. Write the first
    part which asks for both numbers then reads them in with the Scanner class - then print
    them back to the screen so you know the input phase is working.

    Tackle it one piece at a time.

    Wishes Ada xx
    If to Err is human - then programmers are most human of us all.
    "The Analytical Engine offers a new, a vast, and a powerful language . . .
    for the purposes of mankind
    ."
    Augusta Ada Byron, Lady Lovelace (1851)

  11. #11
    Junior Member
    Join Date
    Aug 2014
    Posts
    24
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Need help with programming a simple password checker.

    Hello Ada! I was wondering if you could type out a simple draft of what you are trying to tell me haha I'm sorry but i'm a huge noob at this and i do better with seeing

  12. #12
    Member Ada Lovelace's Avatar
    Join Date
    May 2014
    Location
    South England UK
    Posts
    414
    My Mood
    Angelic
    Thanks
    27
    Thanked 61 Times in 55 Posts

    Default Re: Need help with programming a simple password checker.

    You have more or less done it already in your original post. You asked for one
    input and read it in. Now ask for a second input (use integers if that's all you know
    to this point), then use println methods to print the entered values back to the console.
    Example of possible output:

    System.out.println("first number was: " + number1);

    Once you can do this part, then you need (as suggested by Ozicron) to defined
    two statements that the input can be compared to - test for their equality and
    print out the expected outputs.

    Wishes Ada xx
    If to Err is human - then programmers are most human of us all.
    "The Analytical Engine offers a new, a vast, and a powerful language . . .
    for the purposes of mankind
    ."
    Augusta Ada Byron, Lady Lovelace (1851)

  13. #13
    Junior Member
    Join Date
    Aug 2014
    Posts
    24
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Need help with programming a simple password checker.

    package class1;
    import java.util.Scanner;
    //I had to use scanner in this program because i had to create objects that were in the Scanner class, such as in row 17.
    // The password to this program is anything with 5 or less letters. Numbers are not allowed.
    /**
    * @author Spec *
    * @version 1.0001, 30 August 2014 *
    * This program confirms a password typed into a *
    * console window *
    */

    public class class1
    {
    public static void main(String[] args)
    {
    System.out.println("Password Checker");
    Scanner keyboard = new Scanner(System.in);
    //This is why I needed Scanner
    System.out.println("please enter a username:");
    String username = keyboard.next();
    //I used String because I needed to use a string of characters and not just one integer like int would be good for.

    System.out.print("Please enter a Password: ");
    String password = keyboard.next();
    // I also had to use if else statements to make the computer decide whether or not what the user put in was right or wrong.
    if (isValid(password))
    {
    System.out.println("You are approved by access control!");
    }
    else
    {
    System.out.println("Try again:");
    password = keyboard.next();
    // I basically remade the code up above except if the person gets it wrong, then the program is terminated.
    if (isValid(password))
    {
    System.out.println("You are approved by access control!");
    }
    else
    {
    System.out.println("Sorry");
    }
    }
    }

    this is what i have so far! any help with the rest?

    --- Update ---

    help!

  14. #14
    Member Ada Lovelace's Avatar
    Join Date
    May 2014
    Location
    South England UK
    Posts
    414
    My Mood
    Angelic
    Thanks
    27
    Thanked 61 Times in 55 Posts

    Default Re: Need help with programming a simple password checker.

    Ok... so you cannot use integers now as stated previously? That's fine.
    Strings are more appropriate for this type of problem anyway .

    What you have so far looks good and you have the algorithm worked out more or less.

    I'm guessing "isValid" is going to be a variable of some sort. What you need to do now
    is create two more strings, one to hold the actual correct password and one to hold the
    actual username. Then as you have shown above, check if the entered password matches
    the stored password, and the same with the username.

    Tip: When comparing strings do not use the equality operator '==' read up on Java String
    built-in methods to help you with this part.

    If the input is incorrect, you may want to encase any other input code within a loop statement,
    so the user keeps on entering input until it is correct. Just an idea

    Good luck, your on the right track = ask of you need any more help.

    Wishes Ada xx
    If to Err is human - then programmers are most human of us all.
    "The Analytical Engine offers a new, a vast, and a powerful language . . .
    for the purposes of mankind
    ."
    Augusta Ada Byron, Lady Lovelace (1851)

  15. #15
    Junior Member
    Join Date
    Aug 2014
    Posts
    24
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Need help with programming a simple password checker.

    would i just store the correct password by using String password1 = "password";

    --- Update ---

    I created it so that i can now any 1 character and the password is right but i dont know how to make it a specific password

    --- Update ---

    package Homework1;
    import java.util.Scanner;
    //I had to use scanner in this program because i had to create objects that were in the Scanner class, such as in row 17.
    // The password to this program is anything with 5 or less letters. Numbers are not allowed.
    /**
    * @author Spec *
    * @version 1.0001, 30 August 2014 *
    * This program confirms a password typed into a *
    * console window *
    */

    public class Homework1
    {
    public static void main(String[] args)
    {
    System.out.println("Password Checker");
    Scanner keyboard = new Scanner(System.in);
    //This is why I needed Scanner
    System.out.println("please enter a username:");
    String username = keyboard.next();
    //I used String because I needed to use a string of characters and not just one integer like int would be good for.

    System.out.print("Please enter a Password: ");
    String password = keyboard.next();
    // I also had to use if else statements to make the computer decide whether or not what the user put in was right or wrong.
    if (isValid(password))
    {
    System.out.println("You are approved by access control!");
    }
    else
    {
    System.out.println("Try again:");
    password = keyboard.next();
    // I basically remade the code up above except if the person gets it wrong, then the program is terminated.
    if (isValid(password))
    {
    System.out.println("You are approved by access control!");
    }
    else
    {
    System.out.println("Sorry");
    }
    }
    }


    public static boolean isValid(String password) {
    // If the user enters any character on the keyboard, then the password will be correct.
    //I had to make the password length shorter than how many digits were allowed or else the program wouldn't run. It may have been more code than necessary but it worked out well.

    if (password.length() > 1) {
    return false;
    } else {
    char c;
    int count = 1;
    for (int i = 0; i < password.length() - 1; i++) {
    c = password.charAt(i);
    if (!Character.isLetter(c)) {
    return false;
    } else if (Character.isDigit(c)) {
    count++;
    if (count < 1) {
    return false;
    }
    }
    }
    }
    return true;
    }
    }



    this is my code now but the password will be right for any password that is one character long...how can I make it so it is one single internal password instead of many 1 charcter long passwords....do i even need the public static boolean or can I do it an easier way?

    --- Update ---

    Thank you so much by the way You're really helping me a lot!

  16. #16
    Member Ada Lovelace's Avatar
    Join Date
    May 2014
    Location
    South England UK
    Posts
    414
    My Mood
    Angelic
    Thanks
    27
    Thanked 61 Times in 55 Posts

    Default Re: Need help with programming a simple password checker.

    Lets look at the problem from a different prospective.

    Say the user enters a password, which is stored as a String.

    System.out.print("Please enter your password: ");
    String password  = input.nextLine();

    Before this was written, let's say I had defined two String variables as follows:

    String thePassword ???
    String theUsername ???

    Let's say I assigned a specific String value to each of those. Now, when I test if the
    password entered by the user is correct, I could do this in other languages:

    if (password == thePassword) {
        System.out.println("Yay!!!!");

    but in Java that is incorrect. What you have developed using the Boolean method is (as you say)
    quite long-winded for a simple test case. I would suggest reading up on String methods (Google),
    and then implementing one which will apply to the above code. Once you have this part working,
    you have more or less done the assignment.

    Wishes Ada xx
    If to Err is human - then programmers are most human of us all.
    "The Analytical Engine offers a new, a vast, and a powerful language . . .
    for the purposes of mankind
    ."
    Augusta Ada Byron, Lady Lovelace (1851)

  17. #17
    Junior Member
    Join Date
    Aug 2014
    Posts
    24
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Need help with programming a simple password checker.

    So are you saying I should just get rid of the public static boolean stuff?

    --- Update ---

    I am so confused now

  18. #18
    Member Ada Lovelace's Avatar
    Join Date
    May 2014
    Location
    South England UK
    Posts
    414
    My Mood
    Angelic
    Thanks
    27
    Thanked 61 Times in 55 Posts

    Default Re: Need help with programming a simple password checker.

    Not at all. If what you have created is working - what I am suggesting is modifying your
    algorithm so it looks a bit cleaner and simpler. Using the String approach shown would work
    in your method. As you have set the return type to be Boolean, it could return true if the two
    strings matched and false if they didn't.

    Using the for loop you could place the testing code in the loop body and set the condition
    to terminate repetition if both values are true. Something like this in a while loop fashion:

    Boolean correct = false;
    while (correct != true) {
        // ask for input
     
        // test if input matches expected password
        if (the inputs are both correct, terminate the loop)
            correct = true;
    }

    Something along those lines.

    Wishes Ada xx
    If to Err is human - then programmers are most human of us all.
    "The Analytical Engine offers a new, a vast, and a powerful language . . .
    for the purposes of mankind
    ."
    Augusta Ada Byron, Lady Lovelace (1851)

  19. #19
    Junior Member
    Join Date
    Aug 2014
    Posts
    24
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Need help with programming a simple password checker.

    what do you mean loop fashion? I am very very new at this (like a week into class) so we haven't gone over all of this yet. I am trying to understand but damn am I confused haha

    --- Update ---

    Gah this sucks...I am just working in circles here haha I havent gotten like anything done in 2 days of trying.

  20. #20
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Need help with programming a simple password checker.

    Post your latest code - correctly, in code or highlight tags as explained here, and then update your question. Reviewing the above, I'm not sure what you're working on and what you need help with.

  21. #21
    Junior Member
    Join Date
    Aug 2014
    Posts
    24
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Need help with programming a simple password checker.

    My assignment is this :

    Use Java to write and run a simple console-window program for checking passwords. The program must satisfy the following requirements:
    · When the program runs, it should first print its name, e.g., Password Checker
    · The program should next prompt the user to enter a username, e.g.,
    Please enter your username:
    · The program should next prompt the user to enter a password, e.g.,
    Please enter your password:
    · If the user enters a correct password, i.e., one that matches an internal secret password, the program responds
    You are approved by access control!, and quits.
    · If the user enters an incorrect password, the program responds
    Try again:
    · If the user enters a correct password matching the secret one, the program responds
    You are approved by access control!, and quits.
    · If the user enters an incorrect password, the program responds
    Sorry, and quits.



    package Homework1;
    import java.util.Scanner; 
    //I had to use scanner in this program because i had to create objects that were in the Scanner class, such as in row 16.
    // The password is Tradd
    /**
    * @author Spec *
    * @version 1.0001, 30 August 2014 *
    * This program confirms a password typed into a *
    * console window *
    */
     
    public class hhh 
    { 
    	public static void main(String[] args) {
    		//Needs to add a scanner to the program to continue on
    		Scanner keyboard = new Scanner(System.in);
     
    		//Initial values are set 
    		int tries = 1;
    		String password = "Tradd";
     
    		//print statements that display the programs name 
    		System.out.println("Password Checker");
    		//Ask user to input username 
    		System.out.println("Please enter a Username:");
     
    		//takes username from keyboard input
    		String username = keyboard.next(); 
    		//ask the user to input a secret password
    		System.out.println("Please enter your password:"); 
    		String temp = keyboard.next();
     
    		if(temp.equals(password)) 
    		{
    		System.out.println("You are approved by access control!"); 
                    System.exit(0);
    		}
    		else
    		{
    			System.out.println("Try Again:");		
    			}
     
    		keyboard.next();
     
    		if (temp.equals(password))
    		{
    			System.out.println("You are approved by access control!");
    		}
    		else
    		{
    			System.out.println("Sorry");
    		}
    		System.exit(0);
    	}
    }

    As of right now my code kinda works. The only problem im having is when i tested to get the first password try wrong and the second one right, it still tells me the right password is wrong.
    Last edited by HappyCamper; September 1st, 2014 at 12:26 PM.

  22. #22
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Need help with programming a simple password checker.

    For the second try, the value of 'temp' doesn't change. The line:

    keyboard.next();

    throws away the user's input rather than save it in temp.

  23. #23
    Junior Member
    Join Date
    Aug 2014
    Posts
    24
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Need help with programming a simple password checker.

    actually I think I may have fixed it! can anyone check to see if this works for them?
    package Homework1;
    import java.util.Scanner; 
    //I had to use scanner in this program because i had to create objects that were in the Scanner class, such as in row 16.
    // The password is Tradd.
    /**
    * @author Spec *
    * @version 1.0001, 30 August 2014 *
    * This program confirms a password typed into a *
    * console window *
    */
     
    public class hhh 
    { 
    	public static void main(String[] args) {
    		//Needs to add a scanner to the program to continue on
    		Scanner keyboard = new Scanner(System.in);
     
    		//Initial values are set 
    		int tries = 1;
    		String password = "Tradd";
     
    		//print statements that display the programs name 
    		System.out.println("Password Checker");
    		//Ask user to input username 
    		System.out.println("Please enter a Username:");
     
    		//takes username from keyboard input
    		String username = keyboard.next(); 
    		//ask the user to input a secret password
    		System.out.println("Please enter your password:"); 
    		String temp1 = keyboard.next();
     
    		if(temp1.equals(password)) 
    		{
    		System.out.println("You are approved by access control!"); 
    		System.exit(0);
    		}
    		else
    		{
    			System.out.println("Try Again:");		
    			}
     
    		String temp2 = keyboard.next();
     
    		if (temp2.equals(password))
    		{
    			System.out.println("You are approved by access control!");
    		}
    		else
    		{
    			System.out.println("Sorry");
    		}
    		System.exit(0);
    	}
    }
    I added another temp and it ended up working out. Does this work better?

Similar Threads

  1. [SOLVED] solved
    By Tradd in forum What's Wrong With My Code?
    Replies: 1
    Last Post: August 30th, 2014, 04:03 AM
  2. How to make a very simple Password Strength indicator?
    By blobby404 in forum What's Wrong With My Code?
    Replies: 12
    Last Post: June 18th, 2014, 07:40 PM
  3. JAVA NOOB: Password Strength Checker NEED HELP
    By blobby404 in forum What's Wrong With My Code?
    Replies: 0
    Last Post: June 18th, 2014, 05:55 PM
  4. Simple beginner's password guessing program
    By edishuman in forum What's Wrong With My Code?
    Replies: 3
    Last Post: July 12th, 2011, 03:59 PM
  5. Password Strength Checker
    By uthuth in forum Object Oriented Programming
    Replies: 1
    Last Post: February 6th, 2010, 04:09 PM

Tags for this Thread