943,712 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 2468
  • Java RSS
Nov 18th, 2008
0

Basic Password Verifier

Expand Post »
Hey there, this is my first post here and I'm in the need of some real help.


I've written a basic password verifier, which checks to see if what you type in is the same as the default stored password, but i continually get a compile error.


Java Syntax (Toggle Plain Text)
  1. import java.util.Scanner;
  2.  
  3. public class PasswordVerifier
  4. {
  5. public static boolean isValid(String userPass)
  6. {
  7. /**
  8. * This is the default stored password
  9. */
  10.  
  11. // Password stored by default is "cool"
  12.  
  13. boolean goodSoFar = true; // Flag
  14. int index = 0; // Loop control variable
  15.  
  16. // Is the string the correct length?
  17. if (userPass.length() != 4)
  18. goodSoFar = false;
  19.  
  20.  
  21. // Test to see if the characters match the password
  22. while (goodSoFar && index < 4)
  23. {
  24. if (!Character(userPass.charAt(index == 'c')))
  25. goodSoFar = false;
  26. index++;
  27. if (!Character(userPass.charAt(index == 'o')))
  28. goodSoFar = false;
  29. index++;
  30. if (!Character(userPass.charAt(index == 'o')))
  31. goodSoFar = false;
  32. index++;
  33. if (!Character(userPass.charAt(index == 'l')))
  34. goodSoFar = false;there
  35. index++;
  36.  
  37. //results are returned
  38. return goodSoFar;
  39. }
  40. }
  41. }


And then here is my driver class


Java Syntax (Toggle Plain Text)
  1. import java.util.Scanner;
  2.  
  3. public class PasswordDriver
  4. {
  5.  
  6. public static void main(String[] args)
  7. {
  8. PasswordVerifier PW = new PasswordVerifier();
  9. String user; // To hold a user pass
  10.  
  11.  
  12. while(user != "")
  13. {
  14. System.out.println("Please enter a password containing letters and lowercase only, or nothing to end");
  15. //create a scanner object
  16. Scanner keyboard = new Scanner(System.in);
  17.  
  18. //get user pass
  19. user = keyboard.nextLine();
  20.  
  21. //Determine if the password is valid.
  22. if (PW(user))
  23. {
  24. System.out.println("That is a valid password");
  25. }
  26. else
  27. {
  28. System.out.println("That is not the proper password or format");
  29. System.out.println("Here is an example: cool");
  30. }
  31. }
  32. }
  33. }




The error i get when I'm compiling is

PasswordVerifier.java:30: charAt(int) in java.lang.String cannot be applied to (boolean)
if (!Character(userPass.charAt(index == 'c')))
^
PasswordVerifier.java:33: charAt(int) in java.lang.String cannot be applied to (boolean)
if (!Character(userPass.charAt(index == 'o')))
^
PasswordVerifier.java:36: charAt(int) in java.lang.String cannot be applied to (boolean)
if (!Character(userPass.charAt(index == 'o')))
^
PasswordVerifier.java:39: charAt(int) in java.lang.String cannot be applied to (boolean)
if (!Character(userPass.charAt(index == 'l')))



could someone please help me out, i've tried multiple variances and fixes, but alas... none work for me. Thanks everyone!
Reputation Points: 10
Solved Threads: 0
Newbie Poster
AsantaSane is offline Offline
2 posts
since Nov 2008
Nov 18th, 2008
0

Re: Basic Password Verifier

it needs to be changed to this format
Java Syntax (Toggle Plain Text)
  1. if (userPass.charAt(index) == 'c')

but you are making it hard, you can do this
Java Syntax (Toggle Plain Text)
  1. if(userPass.startsWith("cool")){
  2. return false;
  3. }
Reputation Points: 133
Solved Threads: 141
Veteran Poster
dickersonka is offline Offline
1,162 posts
since Aug 2008
Nov 18th, 2008
0

Re: Basic Password Verifier

startsWith? so "coolblahblahblah" will be true too? not sure that sounds totally secure, but yeah, the compiler error is as stated above.
Reputation Points: 85
Solved Threads: 64
Practically a Master Poster
sillyboy is offline Offline
686 posts
since Mar 2007
Nov 18th, 2008
0

Re: Basic Password Verifier

I think you have misplaced the parenthesis:

This:
if (!Character(userPass.charAt(index == 'c')))

Should be:
if (!Character(userPass.charAt(index) == 'c'))

You could also try:
Java Syntax (Toggle Plain Text)
  1. goodSoFar = userPass.equals("cool");
  2. return goodSoFar;
Sponsor
Featured Poster
Reputation Points: 1014
Solved Threads: 446
Nearly a Senior Poster
javaAddict is offline Offline
3,258 posts
since Dec 2007
Nov 18th, 2008
0

Re: Basic Password Verifier

lol i don't think checking if the password contains c o o l, was too secure, thats why i made the post
Reputation Points: 133
Solved Threads: 141
Veteran Poster
dickersonka is offline Offline
1,162 posts
since Aug 2008
Nov 19th, 2008
0

Re: Basic Password Verifier

thanks dickersonka... that fixed the compile issue for the constructor, but in the drive i'm now getting the following error, yet i see nothing wrong =\


symbol : method PW(java.lang.String)
location: class PasswordDriver
if (PW(user))
Reputation Points: 10
Solved Threads: 0
Newbie Poster
AsantaSane is offline Offline
2 posts
since Nov 2008
Nov 19th, 2008
0

Re: Basic Password Verifier

You already have the "PasswordVerifier" so you now want to use the methods within it.

Java Syntax (Toggle Plain Text)
  1. PW.isValid(user)
Reputation Points: 85
Solved Threads: 64
Practically a Master Poster
sillyboy is offline Offline
686 posts
since Mar 2007
Nov 19th, 2008
0

Re: Basic Password Verifier

wow ... and to think I thought the .equals(..) method worked great
Reputation Points: 919
Solved Threads: 354
Nearly a Posting Maven
stultuske is offline Offline
2,487 posts
since Jan 2007

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: getting errors in java for an assignment
Next Thread in Java Forum Timeline: Sliding window protocol help





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC