Page 1 of 2 12 LastLast
Results 1 to 20 of 28
  1. #1
    DarkAlex is offline Member
    Join Date
    Mar 2011
    Posts
    10
    Rep Power
    0

    Default Simple password validation.

    Hello,
    I am currently enrolled in a low level java class in college as a new computer science major.
    I was wondering about this basic program.

    The goal is to make a program which checks a user's password for 2 things: that it contains 8 characters and that it has a #,$, or %

    Here's what I have so far.
    /*
    * Validates a new password
    * Parameters: must be 8 characters
    * Must contain $ % #
    */
    ------------------------

    import java.util.scanner

    public class CS1_Frazer_X7_2
    {
    public static void main(String[] args)
    {
    String input;

    System.out.println("Please enter new password");

    Scanner kbd = new Scanner;
    input = kbd.nextLine();

    -----------------------
    Those, of course, of the obvious first steps... but what should I do next?

  2. #2
    milovan is offline Senior Member
    Join Date
    Jan 2011
    Location
    Belgrade, Serbia
    Posts
    301
    Rep Power
    4

    Default

    input.length()
    charAt(int)

  3. #3
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Yes, now you want to do the actual validation on the input string. You can find the length of a string with the yourString.length();

    And charAt lets you see what the char at the position supplied is, so charAt(0) would give you the first item in the string. Now you need to use some sort of statement to go through each item and check if they are one of the items you need to be in the password.

  4. #4
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,788
    Rep Power
    7

    Default

    The indexOf method would be more useful than charAt method.

  5. #5
    DarkAlex is offline Member
    Join Date
    Mar 2011
    Posts
    10
    Rep Power
    0

    Default

    Okay then I assume just an if else statement?

    So

    If (input.length() >= 8) {
    System.out.println("Your password has been accepted");
    } else {
    System.out.println("Make a new password");
    }

    Right?

  6. #6
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,788
    Rep Power
    7

    Default

    You are on the right track, however you have to check more than just the length. It might help to use a boolean and several if statements. If the length is less than 8 set the boolean to false. Also do the checks for the required chars. Then at the end if the boolean is true then display the "OK" message else the bad message.

  7. #7
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Yes, but you need to also test for the symbols. indexOf is the way to go, it takes an argument of string and returns a positive number if the item is found, if it isn't it returns a -1.

  8. #8
    DarkAlex is offline Member
    Join Date
    Mar 2011
    Posts
    10
    Rep Power
    0

    Default

    Okay what I don't quite understand is how to make the indexOf thing work.
    What I have been doing is

    if (input.length >= 8) && (input.indexOf('#','$','%')) {
    System.out.println("Password Accepted");
    } else {
    System.out.println("Your password is invalid. Please try again");
    }

    Is there some way to make that a boolean so that if it returns true just that the thing is there, it goes on to the next part?
    Last edited by DarkAlex; 03-20-2011 at 11:14 PM.

  9. #9
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,788
    Rep Power
    7

    Default

    The indexOf method only takes one parameter. You will need to call it 3 times.

  10. #10
    DarkAlex is offline Member
    Join Date
    Mar 2011
    Posts
    10
    Rep Power
    0

    Default

    Quote Originally Posted by Junky View Post
    The indexOf method only takes one parameter. You will need to call it 3 times.
    Is there a way to say "or"?
    For example, if the statement contains #, $, OR % then say "Okay"

  11. #11
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,788
    Rep Power
    7

    Default

    Yes by using the or symbol (just like you did with and) in between the three calls to indexOf. But this can get messy real quick. That is why I suggested using several if statements, one for each check.

  12. #12
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Yes there is ||

  13. #13
    DarkAlex is offline Member
    Join Date
    Mar 2011
    Posts
    10
    Rep Power
    0

    Default

    import java.util.Scanner;

    public class CS1_Frazer_X7_2
    {
    public static void main(String[] args)
    {
    String input;
    int length;

    System.out.println("Please enter new password");

    Scanner k = new Scanner(System.in);
    input = k.nextLine();

    if (input.indexOf('$')==1) {
    System.out.println("Password Approved");
    } else {
    System.out.println("Password Denied");
    if (input.indexOf('%')==1) {
    System.out.println("Password Approved");
    }else{
    System.out.println("Password Denied");
    if (input.indexOf('#')==1) {
    System.out.println("Password approved");
    }else{
    System.out.println("Password Denied");
    if (input.length() >= 8){
    System.out.println("Password Approved");
    } else {
    System.out.println("Password Denied");
    }
    }
    }
    }
    }
    }

    -------------------
    I'm sure you see the problems... but how do I solve them?

  14. #14
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,788
    Rep Power
    7

    Default

    A password of 1234567$ would not pass. Hmmmm!

    Make sure your test you code extensively with as many different combinations as you can think of.

  15. #15
    DarkAlex is offline Member
    Join Date
    Mar 2011
    Posts
    10
    Rep Power
    0

    Default

    Quote Originally Posted by Junky View Post
    A password of 1234567$ would not pass. Hmmmm!

    Make sure your test you code extensively with as many different combinations as you can think of.
    Well what I have in mind right now is that I can use || between all of the other ones and this makes it so that it won't matter WHICH symbol is used.

    But I am totally lost to be honest haha

    I also want to clean it up. 4 instances of "password denied" is a little annoying.

  16. #16
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,788
    Rep Power
    7

    Default

    I just realised that the checks must be done in an if/else if statement. Otherwise a password of 1234567$ would pass the $ check but then fail the # check.

  17. #17
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,788
    Rep Power
    7

    Default

    Quote Originally Posted by DarkAlex View Post
    I also want to clean it up. 4 instances of "password denied" is a little annoying.
    That is why I suggested using a boolean and an extrqa if statement at the end.

    It is hard to provide you with assistance when I would use a method to validate the password but I guess you are not upto using methods yet.

  18. #18
    DarkAlex is offline Member
    Join Date
    Mar 2011
    Posts
    10
    Rep Power
    0

    Default

    Sorry but I honestly don't understand exactly what you're saying.
    I'm assuming if you're saying "a pass in boolean is 1 and a fail is -1" that I should do "input.indexOf("#")==1"
    I know that boolean means true/false statement but that's all I know about it.

    If you could dumb down everything you're saying to about 1 month java experience I might get it better haha

  19. #19
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,788
    Rep Power
    7

    Default

    No.

    The indexOf method returns the position in the String where the target char is located or -1 if it is not found. EG in the String "hello world" the indexOf('w') call would return 6, the indexOf('h') call would return 0 and the indexOf('z') call would return -1.

  20. #20
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,788
    Rep Power
    7

    Default

    Java Code:
    valid set to true
    if length is less than 8
        set valid to false
    else if $ not found
        set valid to false
    else if % not found
        set valid to false
    else if # not found
        set valid to false
    
    if valid
        print OK message
    else 
        print bad message

Page 1 of 2 12 LastLast

Similar Threads

  1. user name and password validation
    By exose in forum New To Java
    Replies: 6
    Last Post: 01-20-2011, 02:59 PM
  2. Password
    By Adomini in forum New To Java
    Replies: 3
    Last Post: 09-20-2010, 10:43 AM
  3. how to check password for 3 times enterd wrong password
    By sk.mahaboobbhasha@gmail.c in forum New To Java
    Replies: 2
    Last Post: 11-14-2008, 07:53 PM
  4. how to check password for 3 times enterd wrong password
    By sk.mahaboobbhasha@gmail.c in forum Java Servlet
    Replies: 0
    Last Post: 11-14-2008, 01:22 PM
  5. How to check password of a jsp/html with the password of Database(mysql) #1
    By sk.mahaboobbhasha@gmail.c in forum Java Servlet
    Replies: 2
    Last Post: 11-14-2008, 01:11 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •