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

    Default small java problem thanks in advance for your help

    i wrote a password check program: rules: length not less that 6 and not more than 1/ not all alphabets/ not all numbers/not alphabets ending with number 99
    if i try the password CGI/a199 it works thought if i try 99 it puts out of bound. any suggestions? and if i type cmps200 at first it puts valid but if i type after example cgi it puts rule 1 and 2 violated.
    import java.util.*;

    public class trial
    {
    public static void main(String[] args)
    {
    Scanner scan = new Scanner (System.in); //using Scanner to input the input from the keyboard
    System.out.println("Please enter your password: "); //asks the user to enter password
    String S = scan.nextLine(); //reads to the line entered by the user
    int len = S.length();

    while (one(S, len)==true || (alphabet(S,len) == true) || (integer(S, len) == true) || (lasttwo(S,len) ==true))
    {

    System.out.print("not a valid password");
    System.out.print(" (rules(s): ");

    if( one(S, len)==true )
    {
    System.out.print("1 ");
    }
    if(alphabet(S,len) == true)
    {
    System.out.print(" 2 ");
    }
    if (integer(S, len) == true)
    {
    System.out.print(" 3 ");
    }
    if (lasttwo(S,len) ==true)
    {
    System.out.print(" 4 ");
    }
    System.out.println(" violated)");
    System.out.print("Please enter your password: "); //asks the user to enter password
    S= scan.nextLine(); //reads to the line entered by the user
    }
    System.out.print("valid password");
    }

    public static boolean one (String S, int len)
    {
    if (len >6 && len < 15) //rule one
    {
    return false;
    }
    return true;
    }

    public static boolean alphabet (String S, int len)
    {

    int intCount = 0;

    for(int i = 0; i < len; i++)
    {


    if (Character.isDigit(S.charAt(i))) //method to check if i is a digit
    {
    intCount++;
    }
    }

    if(intCount == 0 ) //if charCount is equal to zero violation 3 is printed to
    {
    return true;
    }

    return false;
    }


    public static boolean integer (String S, int len)
    {
    int charCount = 0; //initializing values to zero

    for(int i = 0; i < len; i++)
    {

    if (Character.isLetter(S.charAt(i))) //method to check if i is a digit
    {
    charCount++;
    }

    }


    if(charCount== 0 ) //if charCount is equal to zero violation 3 is printed to
    {
    return true;
    }

    return false;
    }

    public static boolean lasttwo (String S, int len)
    {
    int characters = 0;
    if (len ==2)
    {
    return false;
    }
    for(int i=0; i<len-2; i++) //check if last two places in string are '99'
    {
    char y =S.charAt(i);
    if( y>=65 && y<=90 || y>=97 && y<=122)
    {
    characters++;
    }
    }

    if (characters==len-2 &&(S.charAt(len-2) == '9') && (S.charAt(len-1) == '9'))
    {
    return true;
    }

    return false;
    }
    }

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,987
    Rep Power
    9

    Default

    When posting code, make sure you use the CODE tags to preserve formatting. Otherwise nobody will want to read it.

    You should step through your program with a debugger (or at the very least, put in a ton of print statements) to figure out what's going wrong.

  3. #3
    lovelesh is offline Senior Member
    Join Date
    Nov 2010
    Location
    Delhi
    Posts
    135
    Blog Entries
    1
    Rep Power
    0

    Wink

    You need to refactor your code. String class offers a lot of powerful methods which you are not using.
    By using these methods you can reduce the number of lines in your code by atleast 50%. Try using them

    Just for your reference:
    - use endswith method for checking last two characters
    - use Integer.parseInt to check if the string is integer only.
    - In some of the methods you have declared unneccasary parameters

  4. #4
    Muskar is offline Member
    Join Date
    Nov 2010
    Posts
    73
    Rep Power
    0

    Default

    Code formatted by Eclipse's auto-format function:

    Java Code:
    public class trial {
        public static void main(String[] args) {
            Scanner scan = new Scanner(System.in); // using Scanner to input the
                                                    // input from the keyboard
            System.out.println("Please enter your password: "); // asks the user to
                                                                // enter password
            String S = scan.nextLine(); // reads to the line entered by the user
            int len = S.length();
    
            while (one(S, len) == true || (alphabet(S, len) == true)
                    || (integer(S, len) == true) || (lasttwo(S, len) == true)) {
    
                System.out.print("not a valid password");
                System.out.print(" (rules(s): ");
    
                if (one(S, len) == true) {
                    System.out.print("1 ");
                }
                if (alphabet(S, len) == true) {
                    System.out.print(" 2 ");
                }
                if (integer(S, len) == true) {
                    System.out.print(" 3 ");
                }
                if (lasttwo(S, len) == true) {
                    System.out.print(" 4 ");
                }
                System.out.println(" violated)");
                System.out.print("Please enter your password: "); // asks the user
                                                                    // to enter
                                                                    // password
                S = scan.nextLine(); // reads to the line entered by the user
            }
            System.out.print("valid password");
        }
    
        public static boolean one(String S, int len) {
            if (len > 6 && len < 15) // rule one
            {
                return false;
            }
            return true;
        }
    
        public static boolean alphabet(String S, int len) {
    
            int intCount = 0;
    
            for (int i = 0; i < len; i++) {
    
                if (Character.isDigit(S.charAt(i))) // method to check if i is a
                                                    // digit
                {
                    intCount++;
                }
            }
    
            if (intCount == 0) // if charCount is equal to zero violation 3 is
                                // printed to
            {
                return true;
            }
    
            return false;
        }
    
        public static boolean integer(String S, int len) {
            int charCount = 0; // initializing values to zero
    
            for (int i = 0; i < len; i++) {
    
                if (Character.isLetter(S.charAt(i))) // method to check if i is a
                                                        // digit
                {
                    charCount++;
                }
    
            }
    
            if (charCount == 0) // if charCount is equal to zero violation 3 is
                                // printed to
            {
                return true;
            }
    
            return false;
        }
    
        public static boolean lasttwo(String S, int len) {
            int characters = 0;
            if (len == 2) {
                return false;
            }
            for (int i = 0; i < len - 2; i++) // check if last two places in string
                                                // are '99'
            {
                char y = S.charAt(i);
                if (y >= 65 && y <= 90 || y >= 97 && y <= 122) {
                    characters++;
                }
            }
    
            if (characters == len - 2 && (S.charAt(len - 2) == '9')
                    && (S.charAt(len - 1) == '9')) {
                return true;
            }
    
            return false;
        }
    }

  5. #5
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Could you post the complete error message. OutOfBound can be in many places, how we know that? Only the error message help us, or else we have to run your code and fix those. I'm lazy to do that, so do others.

  6. #6
    Join Date
    Jul 2010
    Location
    India
    Posts
    58
    Rep Power
    0

    Default

    Hi Farhm,

    As far as I noticed out of bound exception comes bcoz you are not reassigning the variable "len" ... so it contains the previous data(length of the previous value as in your case the first entered password) giving you the exception . Try adding

    len = S.length();

    immediately after

    S = scan.nextLine(); // reads to the line entered by the user

    just before your while loop ends.


    warm regards
    Vinod M
    ____________________________
    Give me beans .........

  7. #7
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

Similar Threads

  1. small problem, thanks in advance for your help
    By farahm in forum New To Java
    Replies: 4
    Last Post: 11-22-2010, 09:26 AM
  2. small problem
    By rawan in forum Java Applets
    Replies: 2
    Last Post: 04-07-2010, 03:32 AM
  3. Small problem with Stacks
    By SMHouston in forum New To Java
    Replies: 13
    Last Post: 10-16-2009, 02:34 AM
  4. small problem
    By barusk in forum Networking
    Replies: 4
    Last Post: 03-21-2009, 06:19 AM
  5. Small problem
    By ayoood in forum New To Java
    Replies: 2
    Last Post: 06-06-2008, 12:27 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
  •