0

I am supposed to create a method in a different class that takes a string input and: "validate that a String represents 8 symbols, including letters (upper and lower case), numbers, and special symbols like #$&_."

This is my code so far.

public static boolean validSSN(String SSN)
{
    int length = SSN.length();
    boolean flag = false;
    if(length == 11)
    {
        if(SSN.matches("^\\d{3}[-]{1}\\d{2}[-]{1}\\d{4}")) flag = true;
    }
    return flag;
}
GhostCat
  • 127,190
  • 21
  • 146
  • 218
Doddy
  • 9
  • 1
  • 4
    And what is your question and/or problem? – OH GOD SPIDERS Oct 16 '18 at 09:30
  • At first glance 'validate String represents 8 symbols' and you have length == 11 ??? Then just play with regexp, you are here to learn you seem on the right track – PilouPili Oct 16 '18 at 09:35
  • Note 1: `flag = false; if (condition) flag = true;` is better represented by just `flag = condition;` (where condition is any expression resulting in a boolean) – user85421 Oct 16 '18 at 09:46
  • @CarlosHeuberger then " return condition;" would be even better – Stultuske Oct 16 '18 at 09:48
  • Note 2: `length == 11` if you want 8 characters (assuming that is meant by symbols) doesn't make sense – user85421 Oct 16 '18 at 09:48
  • @Stultuske one step a time... [:-) and similar valid for variable `length` – user85421 Oct 16 '18 at 09:48
  • Note 3: your regex does not resemble the specification in any way, is more like you got a code for checking something else (SSN `"ddd-dd-dddd"`), and `"[-]{1}"` is confusing (any character from the char set composed of just a minus, exactly one time? - the same as just `"-"` ) – user85421 Oct 16 '18 at 09:54
  • Welcome to Stack Overflow! Other users marked your question for low quality and need for improvement. I re-worded/formatted your input to make it easier to read/understand. Please review my changes to ensure they reflect your intentions. But I think your question is still not answerable. **You** should [edit] your question now, to add missing details (see [mcve] ). Feel free to drop me a comment in case you have further questions or feedback for me. – GhostCat Oct 16 '18 at 11:10
  • Seems similar: https://stackoverflow.com/a/3802238/7339164 Try to use expression from there. – freeone31 Oct 16 '18 at 11:15

1 Answers1

-1

I may have found my solution to be:

if(pass.matches("^[A-Z|a-z|0-9|#|$|&|_]{8}")) flag =true;

instead of

if(SSN.matches("^\\d{3}[-]{1}\\d{2}[-]{1}\\d{4}")) flag = true;
Doddy
  • 9
  • 1
  • You have no pass variable in your code, how do you think calling 'matches' on a non-existent variable will solve your problem? – Stultuske Oct 16 '18 at 09:37
  • Also,the OP ask a question without detail information and then answer it himself,I think both the answer and the question need to be deleted – lucumt Oct 16 '18 at 09:39
  • And also the `|` symbol is not used in these `[]` brackets as **or** meaning. – Azhy Oct 16 '18 at 09:43
  • Try this instead `^[\w\d\&\$\#]{8}$`. – Azhy Oct 16 '18 at 09:45
  • 1
    If I understood the assignment right, it requires that letters AND digits AND spec chars should be present in the password, all of them, not just any of them. – m. vokhm Oct 16 '18 at 09:50
  • better include the method as whole in this case... because of `SSN`/`pass` and with `length == 11` the above regex will never match (needs 8 characters). – user85421 Oct 16 '18 at 09:59
  • by include I mean in the post, and BTW, there should be no `|` inside `[]` unless you want to match the specific character `'|'`. Inside of `[]` there is an *implicit* OR. Example `"[a-z0-9#]"` will match a lowercase character OR a digit OR a `#` – user85421 Oct 16 '18 at 10:01