Basic Password Verifier

Reply

Join Date: Nov 2008
Posts: 2
Reputation: AsantaSane is an unknown quantity at this point 
Solved Threads: 0
AsantaSane AsantaSane is offline Offline
Newbie Poster

Basic Password Verifier

 
0
  #1
Nov 18th, 2008
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.


  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


  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!
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 1,158
Reputation: dickersonka will become famous soon enough dickersonka will become famous soon enough 
Solved Threads: 136
dickersonka dickersonka is offline Offline
Veteran Poster

Re: Basic Password Verifier

 
0
  #2
Nov 18th, 2008
it needs to be changed to this format
  1. if (userPass.charAt(index) == 'c')

but you are making it hard, you can do this
  1. if(userPass.startsWith("cool")){
  2. return false;
  3. }
Custom Application & Software Development
www.houseshark.net
Reply With Quote Quick reply to this message  
Join Date: Mar 2007
Posts: 683
Reputation: sillyboy is on a distinguished road 
Solved Threads: 60
sillyboy's Avatar
sillyboy sillyboy is offline Offline
Practically a Master Poster

Re: Basic Password Verifier

 
0
  #3
Nov 18th, 2008
startsWith? so "coolblahblahblah" will be true too? not sure that sounds totally secure, but yeah, the compiler error is as stated above.
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 1,525
Reputation: javaAddict is a glorious beacon of light javaAddict is a glorious beacon of light javaAddict is a glorious beacon of light javaAddict is a glorious beacon of light javaAddict is a glorious beacon of light javaAddict is a glorious beacon of light 
Solved Threads: 209
Featured Poster
javaAddict's Avatar
javaAddict javaAddict is offline Offline
Posting Virtuoso

Re: Basic Password Verifier

 
0
  #4
Nov 18th, 2008
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:
  1. goodSoFar = userPass.equals("cool");
  2. return goodSoFar;
Check out my New Bike at my Public Profile at the "About Me" tab
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 1,158
Reputation: dickersonka will become famous soon enough dickersonka will become famous soon enough 
Solved Threads: 136
dickersonka dickersonka is offline Offline
Veteran Poster

Re: Basic Password Verifier

 
0
  #5
Nov 18th, 2008
lol i don't think checking if the password contains c o o l, was too secure, thats why i made the post
Custom Application & Software Development
www.houseshark.net
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 2
Reputation: AsantaSane is an unknown quantity at this point 
Solved Threads: 0
AsantaSane AsantaSane is offline Offline
Newbie Poster

Re: Basic Password Verifier

 
0
  #6
Nov 19th, 2008
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))
Reply With Quote Quick reply to this message  
Join Date: Mar 2007
Posts: 683
Reputation: sillyboy is on a distinguished road 
Solved Threads: 60
sillyboy's Avatar
sillyboy sillyboy is offline Offline
Practically a Master Poster

Re: Basic Password Verifier

 
0
  #7
Nov 19th, 2008
You already have the "PasswordVerifier" so you now want to use the methods within it.

  1. PW.isValid(user)
Reply With Quote Quick reply to this message  
Join Date: Jan 2007
Posts: 706
Reputation: stultuske is a jewel in the rough stultuske is a jewel in the rough stultuske is a jewel in the rough 
Solved Threads: 84
stultuske's Avatar
stultuske stultuske is offline Offline
Master Poster

Re: Basic Password Verifier

 
0
  #8
Nov 19th, 2008
wow ... and to think I thought the .equals(..) method worked great
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the Java Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC