Results 1 to 8 of 8
  1. #1
    farahm is offline Member
    Join Date
    Nov 2010
    Location
    Beirut, Lebanon
    Posts
    36
    Rep Power
    0

    Default code wont enter into loop. thanks in advance for your help

    im writing a password check program.
    i want it to give "a valid password" if the rules are satisfied and "not a valid password" if they are not, along with the violated rules. but it wont enter into the first loop if i put for example "cmps200" which is a correct password it would type "not a valid password" any suggestions(if it helps the rules are length>6 and <15, at least a digit and at least a number and doesnt end with 99 after a sequence of alphabetical letters)
    if(((len > 6) && (len < 15)) && ((charCount > 0)&& (intCount> 0)) && ((charCount > 0) &&((S.charAt(len-1)) != '9' && (S.charAt(len-2) != '9')))) //all rules satisfied
    {
    if ((charCount > 0) && (intCount> 0))
    {
    if ((charCount > 0) && ((S.charAt(len-1) != '9') && (S.charAt(len-2) != '9')))
    {
    System.out.println("a valid password");
    }
    }
    }

    else
    {
    System.out.println("not a valid password");

    if ((len < 6) || (len > 15))
    {
    System.out.println("rule 1 violated");
    }
    i posted up to rule one.

  2. #2
    RamyaSivakanth's Avatar
    RamyaSivakanth is offline Senior Member
    Join Date
    Apr 2009
    Location
    Chennai
    Posts
    835
    Rep Power
    6

    Default

    your if part is confusing ..

    You post a code .Then only we can understand what charCount,intCount and how you are manipulating to come to this if etc we will know.

    It's highly impossible to give solution without knowing head and foot.
    Last edited by RamyaSivakanth; 11-22-2010 at 01:17 PM.
    Ramya:cool:

  3. #3
    farahm is offline Member
    Join Date
    Nov 2010
    Location
    Beirut, Lebanon
    Posts
    36
    Rep Power
    0

    Default

    charCount is used to count if there are any characters and intCount to count if there are any integers
    (used to check that at least there is 1 integer and character) and !9 since i dnt want the last two digits to be nine if it is proceeded by an alphabetical sequence.
    thanks

  4. #4
    RamyaSivakanth's Avatar
    RamyaSivakanth is offline Senior Member
    Join Date
    Apr 2009
    Location
    Chennai
    Posts
    835
    Rep Power
    6

    Default

    You are not getting what Iam saying...Iam able to get your requirement.

    But,my question is you just post your code for getting charCount and intCount.I want to see to correct you.
    Ramya:cool:

  5. #5
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,014
    Rep Power
    20

    Default

    Also use code tags.
    The lack of formatting in that code makes it hard to see the flow.

  6. #6
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,525
    Blog Entries
    7
    Rep Power
    20

    Default

    [QUOTE=farahm;158354]

    You're overcomplicating matters (you have included too many redundant tests and too many parentheses). This should do it:

    Java Code:
    if (len > 6 && len < 15 && charCount > 0 && intCount > 0 && (S.charAt(len-1) != '9' || S.charAt(len-2) != '9')) //all rules satisfied
       System.out.println("a valid password");
    else 
       System.out.println("not a valid password");
    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  7. #7
    farahm is offline Member
    Join Date
    Nov 2010
    Location
    Beirut, Lebanon
    Posts
    36
    Rep Power
    0

    Default

    i figured it out
    what was wrong with the code is that between each condition/rule i should have put || not &&(or not and).

  8. #8
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,525
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by farahm View Post
    i figured it out
    what was wrong with the code is that between each condition/rule i should have put || not &&(or not and).
    It's just boolean logic: let E be the condition that the last character is a '9', let F be the condition that the second to last character is a '9'. So:

    Java Code:
    E: S.charAt(len-1) == '9'
    F: S.charAt(len-2) == '9'
    You don't want both of them to be true (then the String S ends with 99), in other words: !(E&&F) == !E || !F; this translates to Java as:

    Java Code:
    S.charAt(len-1) != '9' || S.charAt(len-2) != '9'
    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. How enter this mode-gui/design-view/code-view
    By lse123 in forum NetBeans
    Replies: 0
    Last Post: 02-28-2010, 09:09 PM
  2. Replies: 18
    Last Post: 11-29-2009, 11:58 PM
  3. Replies: 21
    Last Post: 06-11-2009, 02:18 AM
  4. what code for not hitting enter?
    By tornbacchus in forum New To Java
    Replies: 4
    Last Post: 04-11-2009, 04:59 AM
  5. simple problem - code wont compile
    By dirtycash in forum New To Java
    Replies: 1
    Last Post: 11-20-2007, 05:49 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •