Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Limiting Letters in a String

  1. #1
    Junior Member
    Join Date
    Jul 2013
    Posts
    18
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Limiting Letters in a String

    Why is this code letting me enter a password such as a123?

     
                    System.out.println("\nEnter new password:");
                    strPasswordTemp = input.next();
                    byte[] bytPassword = strPasswordTemp.getBytes();  
                    for (byte bytTemp : bytPassword)
                    {
                        char tempChar = (char) bytTemp;
                        if (Character.isLetter(tempChar))
                        {
                            intNumOfLetters++;
                        } // end of if statement
                        if (Character.isDigit(tempChar))
                        {
                            intNumOfDigits++;
                        } // end of if statement
                    } // end of for loop
                    if (intNumOfLetters < 8 && intNumOfDigits < 3)
                    {
                        System.out.println("\n[ Password must contain at least 8 " +
                            "letters and 3 numbers ]");
                        System.out.println("\nPlease press the 'Enter' key to " +
                                "continue");
                        keyIn.nextLine();
                        break;
                    } // end of if statement


  2. #2
    Member
    Join Date
    Jul 2013
    Location
    Franklin, TN
    Posts
    47
    Thanks
    3
    Thanked 4 Times in 4 Posts

    Default Re: Limiting Letters in a String

    Use System.out.println to see what your input is and make sure everything is getting read correctly. Next do the same with intNumOfLetters and intNumOfDigits, to see if your variable values are wrong or if you are incorrectly scanning the string.

    When you determine the location of where your program is going wrong, the solution becomes much more simple to look for. Tis more efficient to ask how to fix this, than to ask what's wrong in the first place.

  3. #3
    Member Andrew R's Avatar
    Join Date
    Jul 2013
    Location
    San Pedro Sula, Honduras
    Posts
    58
    My Mood
    Inspired
    Thanks
    11
    Thanked 1 Time in 1 Post

    Default Re: Limiting Letters in a String

    I see what you are doing. You are obtaining a String, changing it to byte, then changing the byte to char...

    Could it be thats missing the array state of [] ?

    I think there are easier approaches to this. You can proably get the String length and declare an array of char with that length. Then evalaute position by position.

  4. #4
    Member
    Join Date
    Jul 2013
    Posts
    219
    Thanks
    0
    Thanked 18 Times in 17 Posts

    Default Re: Limiting Letters in a String

    Hello.
    Your condition has logical error.
    Lets say there are two conditions c1 and c2. If either condition is false then the result is false. In this case we will use AND operator.
    But I use converse !c1 and !c2. In this case I will use OR operator not AND.

    Syed.

  5. #5
    Junior Member
    Join Date
    Sep 2013
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Limiting Letters in a String

    yep that's logical error

Similar Threads

  1. [SOLVED] One string Text message into array of strings(letters)
    By dianac in forum What's Wrong With My Code?
    Replies: 20
    Last Post: April 12th, 2013, 07:23 PM
  2. [SOLVED] Double key Caesar Cipher (USING A STACK AND A QUEUE TO SHIFT LETTERS OF A STRING)
    By Medo Almasry in forum What's Wrong With My Code?
    Replies: 16
    Last Post: November 19th, 2011, 04:18 PM
  3. Java Web Service Client Authentication (limiting the number of failed attempts)
    By pranay_reddy in forum JavaServer Pages: JSP & JSTL
    Replies: 1
    Last Post: September 19th, 2011, 04:30 PM
  4. [SOLVED] Replacing letters in a string NOT WORKING.
    By sp11k3t3ht3rd in forum What's Wrong With My Code?
    Replies: 8
    Last Post: January 14th, 2011, 06:38 PM
  5. Limiting decimal places in a double
    By darek9576 in forum Object Oriented Programming
    Replies: 2
    Last Post: March 14th, 2010, 11:27 AM