Need Help on how get started in Password Question

  • (3 Pages)
  • +
  • 1
  • 2
  • 3

38 Replies - 1604 Views - Last Post: 13 July 2011 - 03:24 PM Rate Topic: -----

#1 Lillypop  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 50
  • Joined: 11-July 11

Need Help on how get started in Password Question

Posted 11 July 2011 - 02:42 AM

Hello Everyone,
I'm New to Java Programming
I just want to ask can i answer this qs using loops and if statments and string or i should have a class and methods
I JUST WANT TO SHOW YOU THE QS AND WHAT DO YOU THINK BEST WAY TO ANSWER IT

Here is the qs:

Quote

Write a program to verify passwords. A valid password should have at least 2 Capital Letters, at least 3 digits, no funny characters (no space, . , *, ( , ) etc etc..) only digits and letters. The passwords length should also be greater than 10.
Print out the correct error message to the user.


Therefore, read the qs and then provide me with your suggestion :P

THANK YOU ALL

This post has been edited by Lillypop: 11 July 2011 - 02:44 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Need Help on how get started in Password Question

#2 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2720
  • View blog
  • Posts: 11,444
  • Joined: 20-September 08

Re: Need Help on how get started in Password Question

Posted 11 July 2011 - 03:19 AM

Look at doing it with java.util.regex.Pattern - it would be one line of code
Was This Post Helpful? 2
  • +
  • -

#3 cfoley  Icon User is offline

  • Cabbage
  • member icon

Reputation: 1997
  • View blog
  • Posts: 4,149
  • Joined: 11-December 07

Re: Need Help on how get started in Password Question

Posted 11 July 2011 - 03:43 AM

Not if he needs different error messages. I actually like the way this exercise is set up. Too many web sites give error messages to the effect "Something is wrong with your password. Here is a paragraph with all of our password rules".

How you approach the problem depends on the current focus of your course. If your instructor is just wanting to make sure you can get logic to flow nicely you can simply ask for the password and then run it through some if statements.

A more modular and reusable solution if you're writing an application is to have a password validator class to which you pass the password in the constructor. Then you can have methods such as hasTwoCapitalLetters() and hasThreeDigits()

If you are dealing with a system where the rules might change, or you're writing an API where you don't know the rules in advance you might consider something like an abstract class or interface for PasswordRule. You could have a boolean method passes() to see of the rule is passed and another String method failErrorMessage() which gives you the error message on failing. You would still have your PassowrdValidator class. This time it would store all the PasswordRule instances. There would be a method passesAllRules() and another for getAllErrorMessages().

There are undoubtedly loads of other options but you have to choose one appropriate to what you're trying to achieve. The last one I wrote about here is overkill for a quick class. You probably want the first or the second. The one-line-of-regex-password-validator is popular with programmers because you can reduce your code to a single line. However, it's terrible for the users of your program because it can't give helpful error messages.
Was This Post Helpful? 3
  • +
  • -

#4 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2720
  • View blog
  • Posts: 11,444
  • Joined: 20-September 08

Re: Need Help on how get started in Password Question

Posted 11 July 2011 - 04:27 AM

Quote

However, it's terrible for the users of your program because it can't give helpful error messages.


That's true, and a good point
Was This Post Helpful? 0
  • +
  • -

#5 cfoley  Icon User is offline

  • Cabbage
  • member icon

Reputation: 1997
  • View blog
  • Posts: 4,149
  • Joined: 11-December 07

Re: Need Help on how get started in Password Question

Posted 11 July 2011 - 04:38 AM

Nothing wrong with using regex for the individual rules though. Often it will be the simplest solution. :)
Was This Post Helpful? 1
  • +
  • -

#6 falcon24  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 16
  • Joined: 10-June 11

Re: Need Help on how get started in Password Question

Posted 11 July 2011 - 05:31 AM

where 'ch' is any character
try using this :
Character.isLetter(ch);//checks whether the character is a letter
Character.isDigit(ch);//checks whether the character is a digit
Was This Post Helpful? 1
  • +
  • -

#7 Lillypop  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 50
  • Joined: 11-July 11

Re: Need Help on how get started in Password Question

Posted 11 July 2011 - 09:02 AM

We didn't study regex.

This is my first course with java
and we covered:
1) loops
2) strings
3) classes and method
and this week we are covering arrays
therefore , i can't use what you've said:

is their is a way to do it by loops and if statement correct

BUT IN MIND I'M JUST A BEGINNER AND NEED TO BE DONE ONLY WITH THE SKILLS I'VE LEARNED :)


but THANKS for the quick reply pals
Was This Post Helpful? 0
  • +
  • -

#8 CasiOo  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1398
  • View blog
  • Posts: 3,097
  • Joined: 05-April 11

Re: Need Help on how get started in Password Question

Posted 11 July 2011 - 09:14 AM

View PostLillypop, on 11 July 2011 - 09:02 AM, said:

We didn't study regex.

This is my first course with java
and we covered:
1) loops
2) strings
3) classes and method
and this week we are covering arrays
therefore , i can't use what you've said:

is their is a way to do it by loops and if statement correct

BUT IN MIND I'M JUST A BEGINNER AND NEED TO BE DONE ONLY WITH THE SKILLS I'VE LEARNED :)


but THANKS for the quick reply pals


Use what falcon24 suggested.
You will need:
1 loop
String.charAt( index )
Character.isDigit( char )
Character.isLetter( char )
Character.isUpperCase( char )

That should be able to do it :) then you dont have to check if the character is in the range 'a' to 'Z' which is pretty annoying
Was This Post Helpful? 1
  • +
  • -

#9 cfoley  Icon User is offline

  • Cabbage
  • member icon

Reputation: 1997
  • View blog
  • Posts: 4,149
  • Joined: 11-December 07

Re: Need Help on how get started in Password Question

Posted 11 July 2011 - 09:24 AM

Thanks for the info. I'd recommend the way I suggested with methods like containsTwoCharacters() containsThreeNumbers() etc...
Was This Post Helpful? 0
  • +
  • -

#10 cfoley  Icon User is offline

  • Cabbage
  • member icon

Reputation: 1997
  • View blog
  • Posts: 4,149
  • Joined: 11-December 07

Re: Need Help on how get started in Password Question

Posted 11 July 2011 - 09:35 AM

I suppose I should give a reason here. If you go with my suggestion, you will end up with a loop in each method. However, the logic will be simple and easy to follow (which means less chance of hours of debugging). The rules can be easily added and deleted and the main validation routine will be simple and easy to read. There may also be other subtle differences like the JVM optimising your code better but you're unlikely to see the difference with this code.
Was This Post Helpful? 1
  • +
  • -

#11 Lillypop  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 50
  • Joined: 11-July 11

Re: Need Help on how get started in Password Question

Posted 11 July 2011 - 11:20 AM

Thanks for the help both of you
and now I'll be writing the problem
I'm just having little difficulty on how to start writing
but hopefully I will, I'll post any code if i need help :)
Was This Post Helpful? 0
  • +
  • -

#12 Lillypop  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 50
  • Joined: 11-July 11

Re: Need Help on how get started in Password Question

Posted 11 July 2011 - 12:00 PM

Hey there !! :)
I have a question
can't for this question I do a class for it by having constructors , parameters
and using public boolean isUpperCase and public boolean isDigit???

Guide me guys if i can do it this way :)
because it had just poped in my mind by what I'm confused is how
to i check if the string is greater than 10 in the class ???

Was This Post Helpful? 0
  • +
  • -

#13 ipushmycar  Icon User is offline

  • D.I.C Regular

Reputation: 86
  • View blog
  • Posts: 390
  • Joined: 29-August 10

Re: Need Help on how get started in Password Question

Posted 11 July 2011 - 12:08 PM

I really do not think a class is necessary. I think cfoley's way would be the best. Have different methods to make sure it meets all the prerequisites and if it doesn't display the proper error message.

Just an FYI strings have a .length() method.
Was This Post Helpful? 0
  • +
  • -

#14 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon


Reputation: 7747
  • View blog
  • Posts: 13,105
  • Joined: 19-March 11

Re: Need Help on how get started in Password Question

Posted 11 July 2011 - 12:10 PM

Well, you could see if there's any way to check a String's length. Look at the String class documentation and see if there's a method that might work for you.
I think you'll find something in there.
Was This Post Helpful? 0
  • +
  • -

#15 cfoley  Icon User is offline

  • Cabbage
  • member icon

Reputation: 1997
  • View blog
  • Posts: 4,149
  • Joined: 11-December 07

Re: Need Help on how get started in Password Question

Posted 11 July 2011 - 03:46 PM

Yup. The Character class is good too. There are methods that let you check if a character is a digit or a letter.

This post has been edited by cfoley: 11 July 2011 - 03:46 PM

Was This Post Helpful? 0
  • +
  • -

  • (3 Pages)
  • +
  • 1
  • 2
  • 3