Results 1 to 20 of 21
Thread: 'Password' help
- 12-11-2008, 10:45 PM #1
Member
- Join Date
- Nov 2008
- Posts
- 30
- Rep Power
- 0
'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."); } } }
-
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.
- 12-11-2008, 11:16 PM #3
Member
- Join Date
- Nov 2008
- Posts
- 30
- Rep Power
- 0
could you show a simple one..?
or what it would look like?
- 12-12-2008, 12:50 AM #4
Member
- Join Date
- Dec 2008
- Posts
- 30
- Rep Power
- 0
This is a basic loop. To get three iterations, it is more common to use: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); }
Java Code:for (int index = 0; index < 3; index++){ System.out.println(index); }
- 12-12-2008, 01:31 AM #5
== != .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:
Use the String methods .equals(), .compareTo(), etc.Java Code:if (userPass == compPass)
Luck,Java Code:if (userPass.equals(compPass))
CJSLChris S.
Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.
- 12-12-2008, 02:35 AM #6
Member
- Join Date
- Nov 2008
- Posts
- 30
- Rep Power
- 0
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); }
- 12-12-2008, 03:24 AM #7
Member
- Join Date
- Dec 2008
- Posts
- 5
- Rep Power
- 0
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
- 12-12-2008, 06:43 AM #8
Member
- Join Date
- Dec 2008
- Posts
- 30
- Rep Power
- 0
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
is the same thing asJava Code:for (int index = 0; index < 3; index++){ System.out.println(index); }
Java Code:System.out.println("0"); System.out.println("1"); System.out.println("2");
- 12-12-2008, 08:48 PM #9
Senior Member
- Join Date
- Nov 2007
- Posts
- 160
- Rep Power
- 6
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 08:26 AM.
- 12-12-2008, 09:32 PM #10
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,
CJSLChris S.
Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.
- 12-12-2008, 09:58 PM #11
Member
- Join Date
- Nov 2008
- Posts
- 30
- Rep Power
- 0
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-2008, 10:12 PM #12
Member
- Join Date
- Dec 2008
- Posts
- 30
- Rep Power
- 0
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"); }
- 12-12-2008, 10:34 PM #13
Senior Member
- Join Date
- Nov 2007
- Posts
- 160
- Rep Power
- 6
@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 08:10 AM.
-
- 12-17-2008, 04:17 PM #15
Member
- Join Date
- Nov 2008
- Posts
- 30
- Rep Power
- 0
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? (:
- 12-17-2008, 05:10 PM #16
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 05:11 PM. Reason: forgot to increment the counter
- 12-17-2008, 05:17 PM #17
Senior Member
- Join Date
- Nov 2007
- Posts
- 160
- Rep Power
- 6
The 'break' exits the for loop.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"); }Last edited by carderne; 12-18-2008 at 08:24 AM.
- 12-17-2008, 05:23 PM #18
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().
- 12-17-2008, 06:59 PM #19
Senior Member
- Join Date
- Nov 2007
- Posts
- 160
- Rep Power
- 6
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 08:22 AM.
- 12-17-2008, 08:36 PM #20
Similar Threads
-
how to check password for 3 times enterd wrong password
By sk.mahaboobbhasha@gmail.c in forum New To JavaReplies: 2Last Post: 11-14-2008, 07:53 PM -
how to check password for 3 times enterd wrong password
By sk.mahaboobbhasha@gmail.c in forum Java ServletReplies: 0Last Post: 11-14-2008, 01:22 PM -
password ?!
By jon80 in forum New To JavaReplies: 9Last Post: 11-14-2008, 01:19 PM -
How to check password of a jsp/html with the password of Database(mysql) #1
By sk.mahaboobbhasha@gmail.c in forum Java ServletReplies: 2Last Post: 11-14-2008, 01:11 PM -
How to get password in SWT
By Java Tip in forum SWTReplies: 0Last Post: 07-02-2008, 08:04 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks