Page 1 of 2 12 LastLast
Results 1 to 20 of 21

Thread: 'Password' help

  1. #1
    iWonder is offline Member
    Join Date
    Nov 2008
    Posts
    30
    Rep Power
    0

    Default 'Password' help

    This program is supposed to ask the user for a password..
    and if they guess correctly it will say "Welcome"
    but if they dont get it, they are given a total of 3 tries to guess the password.
    if after 3 tries, they still fail to guess it.. it should say 'access denied'

    any help would be appreciated...

    Java Code:
    import java.util.Scanner;
    
    public class Ch6Ex17 {
      public static void main (String [] args) {
        //declare variables
        String userPass, compPass;
        //initialize scanner
        Scanner input = new Scanner (System.in);
        
        
       System.out.println("Enter the password: ");
        userPass = input.nextLine();
        input.close();
        compPass = "password";
        
        if (userPass == compPass) {
          System.out.println("Welcome.");
        } else {
          System.out.println("Access Denied.");
        }
      }
    }

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    When I see something like XXX tries, I automatically think "loop" such as a for loop or while loop. I suggest you think of the same. Good luck.

  3. #3
    iWonder is offline Member
    Join Date
    Nov 2008
    Posts
    30
    Rep Power
    0

    Default

    could you show a simple one..?
    or what it would look like?

  4. #4
    uncommon is offline Member
    Join Date
    Dec 2008
    Posts
    30
    Rep Power
    0

    Default

    Java Code:
    //loop will count to 3
    //index is initialized to 1
    //loop doesn't run when index > three (before loop runs)
    //add one to the index (at end of loop)
    
      for (int index = 1; index <= 3; index++){
          System.out.println(index);
      }
    This is a basic loop. To get three iterations, it is more common to use:

    Java Code:
      for (int index = 0; index < 3; index++){
          System.out.println(index);
      }

  5. #5
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    8

    Default == != .equals

    Also, and very important (and even more important in this case), you don't want to compare strings using "==". It can/will give wrong results:
    Java Code:
    if (userPass == compPass)
    Use the String methods .equals(), .compareTo(), etc.
    Java Code:
    if (userPass.equals(compPass))
    Luck,
    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  6. #6
    iWonder is offline Member
    Join Date
    Nov 2008
    Posts
    30
    Rep Power
    0

    Default

    Thanks!
    but I have one question...
    What is the use of 'index' in the loop??
    Java Code:
    for (int index = 0; index < 3; index++){
          System.out.println(index);
      }

  7. #7
    thekrazykid is offline Member
    Join Date
    Dec 2008
    Posts
    5
    Rep Power
    0

    Default

    index is just a variable assigned to be a counter.. the for loop is saying for every value of a variable (index) such that a constraint is true (index <3) do this (System.out.println(index)) then once done do something to the counter (index++) before the next iteration

  8. #8
    uncommon is offline Member
    Join Date
    Dec 2008
    Posts
    30
    Rep Power
    0

    Default

    A lot of times, the index is represented as i. It's just used for counting purposes. I think thekrazykid explained it well enough if you break it down into pieces

    Java Code:
    for (int index = 0; index < 3; index++){
          System.out.println(index);
      }
    is the same thing as

    Java Code:
     System.out.println("0");
     System.out.println("1");
     System.out.println("2");

  9. #9
    carderne is offline Senior Member
    Join Date
    Nov 2007
    Posts
    160
    Rep Power
    8

    Default

    Java Code:
    for (int i = 1; i <= 3; i++) {
         //your code here
         if (enteredPass.equals(actualPass)) {
            System.out.println("Welcome");
    
    
        else if (i == 3)
            System.out.println("Access denied");
    }
    Last edited by carderne; 12-18-2008 at 09:26 AM.

  10. #10
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    8

    Default Curly brackets

    @carderne - it's a good idea to use curly brackets "{}"after if, else and for statements even when they're one liners. This way, statements can be added afterwards without any problem.

    Luck,
    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  11. #11
    iWonder is offline Member
    Join Date
    Nov 2008
    Posts
    30
    Rep Power
    0

    Default

    thank you carderne,
    but I was also wondering..
    with every failed attempt it should say "You have typed the incorrect password"
    ONLY when the 3 tries are used should it say "Access denied."

    how would that look like?

  12. #12
    uncommon is offline Member
    Join Date
    Dec 2008
    Posts
    30
    Rep Power
    0

    Default

    You kind of need to figure that one out one your own. Look up conditionals and loops to figure it out. His code cleaned up looks like this:

    Java Code:
    //Loop through three times
    for (int i = 1; i <= 3; i++) {
    	//Compare the passwords
    	if (enteredPass.equals(actualPass))
    		//They picked the right password
    		System.out.println("Welcome");
    	//They failed three times
    	else if (i == 3)
    		System.out.println("Access denied");
    }

  13. #13
    carderne is offline Senior Member
    Join Date
    Nov 2007
    Posts
    160
    Rep Power
    8

    Default

    @CJSL
    Ok, I'll start doing that from now on.

    @iWonder
    After the else if statement in my previous post, add the following:
    else {
    System.out.println("You have entered the incorrect password");
    }
    Last edited by carderne; 12-13-2008 at 09:10 AM.

  14. #14
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by CJSLMAN View Post
    @carderne - it's a good idea to use curly brackets "{}"after if, else and for statements even when they're one liners. This way, statements can be added afterwards without any problem.
    Yeah, I'm sure that I'm not the only one who's been burned by that a time or two.

  15. #15
    iWonder is offline Member
    Join Date
    Nov 2008
    Posts
    30
    Rep Power
    0

    Default

    its all working fine,
    BUT theres just one more thing -_-
    if the password is guessed correctly on the 1st or 2nd try, i dont want the loop to start all over again, saying "enter the password"
    i just need it to say 'welcome' without anything else
    i dont know if im making sens :S

    help please? (:

  16. #16
    xcallmejudasx's Avatar
    xcallmejudasx is offline Senior Member
    Join Date
    Oct 2008
    Location
    Houston, TX & Flint, MI
    Posts
    609
    Rep Power
    7

    Default

    your probably better off using a while loop with 2 conditionals.

    Java Code:
    while((!correct) && (tries<=3)){
    ask for password input
    tries++;
    if(input.equalsIgnoreCase(password){
    correct = true;
    }
    }
    Last edited by xcallmejudasx; 12-17-2008 at 06:11 PM. Reason: forgot to increment the counter

  17. #17
    carderne is offline Senior Member
    Join Date
    Nov 2007
    Posts
    160
    Rep Power
    8

    Default

    Java Code:
    for (int i = 1; i <= 3; i++) {
         //your code here
         if (enteredPass.equals(actualPass)) {
            System.out.println("Welcome");
            break;//this exits the for loop
    
        else if (i == 3)
            System.out.println("Access denied");
    
    else
            System.out.println("Incorrect password");
    }
    The 'break' exits the for loop.
    Last edited by carderne; 12-18-2008 at 09:24 AM.

  18. #18
    xcallmejudasx's Avatar
    xcallmejudasx is offline Senior Member
    Join Date
    Oct 2008
    Location
    Houston, TX & Flint, MI
    Posts
    609
    Rep Power
    7

    Default

    Might want to point out. someString.equals(someotherString) won't ever return true(I believe). Something about it comparing the reference to the string and not the actual string. Your better off using .equalsIgnoreCase() or if your password is case sensitive loop through comparing the .charAt(). I think you can compare characters using .equals().

  19. #19
    carderne is offline Senior Member
    Join Date
    Nov 2007
    Posts
    160
    Rep Power
    8

    Default

    callmejudas,
    No, .equals() is exactly what he needs to use. The problem you're thinking of is if you use '==' to compare two Strings, in which case it will only return true if the two Strings are exact copies of one another.
    Last edited by carderne; 12-18-2008 at 09:22 AM.

  20. #20
    xcallmejudasx's Avatar
    xcallmejudasx is offline Senior Member
    Join Date
    Oct 2008
    Location
    Houston, TX & Flint, MI
    Posts
    609
    Rep Power
    7

    Default

    Quote Originally Posted by carderne View Post
    callmejudas,
    No, .equals() is exactly what he needs to use. The problem you're thinking of is if you use '==' to compare to Strings, in which case it will only return true if the two Strings are exact copies of one another.
    ahh thanks for clearing that up. I have all these rules floating around and not exactly sure where they apply.

Page 1 of 2 12 LastLast

Similar Threads

  1. 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, 08:53 PM
  2. 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, 02:22 PM
  3. password ?!
    By jon80 in forum New To Java
    Replies: 9
    Last Post: 11-14-2008, 02:19 PM
  4. 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, 02:11 PM
  5. How to get password in SWT
    By Java Tip in forum SWT Tips
    Replies: 0
    Last Post: 07-02-2008, 09:04 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
  •