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 9 of 9

Thread: Check Password Program

  1. #1
    Junior Member
    Join Date
    Sep 2011
    Posts
    24
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Cool Check Password Program

    Some Websites impose certain rules for passwords. Write a method that checks whether a string is a valid password. Suppose the password rule is as follows:

    • A password must have at least 8 characters
    • A password consists of only letters and digits
    • A password must contain at least 2 digits

    Write a program that prompts the user to enter a password and displays "Valid Password" if the rule is followed or "Invalid Password" otherwise.

    Here is my code:
    import java.util.*;
    import java.lang.String;
    import java.lang.Character;
     
    public class CheckingPassword {
    	public static void main(String[] args) {
    		Scanner input = new Scanner(System.in);
    		System.out.print("Please enter a Password: ");
    		String password = input.next();
    		if (isValid(password)) {
    			System.out.println("Valid Password");
    		} else {
    			System.out.println("Invalid Password");
    		}
    	}
    	public static boolean isValid(String password) {
    		if (password.length() < 8) { 
    			return false;
    		}	
    	 	char c;  
    		for (int i = 0; i < password.length() - 1; i++) {
    			c = password.charAt(i);
    		}
    		if (c.isLetterOrDigit()) {		
    			return false;
    		} else if (c.isDigit()) {	
    			return false;
    		} else {	
    			return true;
    		}
    	}
    }

    When I compile my code, I get the following error message:
    CheckingPassword.java:29: char cannot be dereferenced
    if (c.isLetterOrDigit()) {
    ^
    CheckingPassword.java:31: char cannot be dereferenced
    } else if (c.isDigit()) {
    ^
    2 errors
    How do I fix this error? Even if I fix this error, my code won't run correctly. I know there is something wrong with my isValid method. I can't quite figure it out. Also I don't know how to check to see if the password has at least 2 digits. How would I do that?


  2. #2
    Forum VIP
    Join Date
    Oct 2010
    Posts
    275
    My Mood
    Cool
    Thanks
    32
    Thanked 54 Times in 47 Posts
    Blog Entries
    2

    Default Re: Check Password Program

    1: char != Character.. a quick look at the Character API and you would see that the isDigit and isLetterOrDigit would be statically called like
    Character.isLetterOrDigit(c)
    or
    Character.isDigit(c)
    2: Are you sure it is not valid if it contains a letter or digit?
    3: Shouldn't the validity checks be in the loop?
    4: Return false if any validity checks don't work... then return true at the end if you haven't returned false already

  3. #3
    Junior Member
    Join Date
    Sep 2011
    Posts
    24
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: Check Password Program

    Thanks, I fixed up those parts of my code.

    Here is the updated code:
    import java.util.*;
    import java.lang.String;
    import java.lang.Character;
     
    public class CheckingPassword {
    	public static void main(String[] args) {
    		Scanner input = new Scanner(System.in);
    		System.out.print("Please enter a Password: ");
    		String password = input.next();
    		if (isValid(password)) {
    			System.out.println("Valid Password");
    		} else {
    			System.out.println("Invalid Password");
    		}
    	}
    	public static boolean isValid(String password) {
    		if (password.length() < 8) { 
    			return false;
    		} else {	
    	 		char c;  
    			for (int i = 0; i < password.length() - 1; i++) {
    				c = password.charAt(i);
    				if (!Character.isLetterOrDigit(c)) {		
    					return false;
    				} else if (Character.isDigit(c)) {	
    					return false;
    				} else {	
    					return true;
    				}
    			}
    		}
    		return true;
    	}
    }
    How would I check to make sure the password contains at least 2 digits?

  4. #4
    Junior Member
    Join Date
    Dec 2011
    Posts
    18
    My Mood
    Grumpy
    Thanks
    0
    Thanked 3 Times in 3 Posts

    Default Re: Check Password Program

    I would add in a counter and use the isDigit() method and if there is less than 2 digits, return false.

    More than likely not the best solution.. I have 3 semesters of java over me in college so I'm still a beginner also!

  5. #5
    Forum VIP
    Join Date
    Oct 2010
    Posts
    275
    My Mood
    Cool
    Thanks
    32
    Thanked 54 Times in 47 Posts
    Blog Entries
    2

    Default Re: Check Password Program

    That what the length() method of string is for, compare that to the minimum length and voila, you have that validity check.

    Also, remember you return false if any characters are not right, but not if any character is correct. If nothing is wrong then you can return true. The part I am talking about is bolded below


    if (!Character.isLetterOrDigit(c)) {
    return false;
    } else if (Character.isDigit(c)) {
    return false;
    } else {
    return true;
    }


  6. #6
    Junior Member
    Join Date
    Sep 2011
    Posts
    24
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: Check Password Program

    Here's a more updated version of my code:
    import java.util.*;
    import java.lang.String;
    import java.lang.Character;
     
    public class CheckingPassword {
    	public static void main(String[] args) {
    		Scanner input = new Scanner(System.in);
    		System.out.print("Please enter a Password: ");
    		String password = input.next();
    		if (isValid(password)) {
    			System.out.println("Valid Password");
    		} else {
    			System.out.println("Invalid Password");
    		}
    	}
    	public static boolean isValid(String password) {
    		if (password.length() < 8) { 
    			return false;
    		} else {	
    	 		char c;
    			int count = 1; 
    			for (int i = 0; i < password.length() - 1; i++) {
    				c = password.charAt(i);
    				if (!Character.isLetterOrDigit(c)) {		
    					return false;
    				} else if (Character.isDigit(c)) {
    					count++;
    					if (count < 2)	{	
    						return false;
    					}	
    				}
    			}
    		}
    		return true;
    	}
    }
    @ppme I added in a counter and it's still not working. I don't think I'm using the counter correctly. How would I fix it?

    @Tjstretch I'm not sure I understand. I deleted the bolded part. Is that what I'm supposed to do?

  7. #7
    Junior Member
    Join Date
    Sep 2011
    Posts
    24
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: Check Password Program

    This topic has just been cross posted here.

    I got the code to work now.

  8. #8
    Junior Member
    Join Date
    Dec 2011
    Posts
    18
    My Mood
    Grumpy
    Thanks
    0
    Thanked 3 Times in 3 Posts

    Default Re: Check Password Program

    Quote Originally Posted by m2msucks View Post
    Here's a more updated version of my code:
    import java.util.*;
    import java.lang.String;
    import java.lang.Character;
     
    public class CheckingPassword {
    	public static void main(String[] args) {
    		Scanner input = new Scanner(System.in);
    		System.out.print("Please enter a Password: ");
    		String password = input.next();
    		if (isValid(password)) {
    			System.out.println("Valid Password");
    		} else {
    			System.out.println("Invalid Password");
    		}
    	}
    	public static boolean isValid(String password) {
    		if (password.length() < 8) { 
    			return false;
    		} else {	
    	 		char c;
    			int count = 0; 
    			for (int i = 0; i < password.length() - 1; i++) {
    				c = password.charAt(i);
    				if (!Character.isLetterOrDigit(c)) {		
    					return false;
    				} else if (Character.isDigit(c)) {
    					count++
    					}	
    				}
    			if (count < 2 )	{	
    			 return false;
                           }
    		}
    		return true;
    	}
    }
    @ppme I added in a counter and it's still not working. I don't think I'm using the counter correctly. How would I fix it?

    @Tjstretch I'm not sure I understand. I deleted the bolded part. Is that what I'm supposed to do?
    Don't have the counter checker within the loop, because otherwise on the first char check, it checks the count <2 and finds it is false, then returns false, and the method terminates.

    I see you have it done, but still if its something you didn't know, its good to know . When you return true or false, at any point in a Boolean method the method terminates

  9. #9
    Junior Member
    Join Date
    Sep 2011
    Posts
    24
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: Check Password Program

    I didn't know that before. Thanks.

Similar Threads

  1. Java Program to Check whether a Expression is Valid using Stack
    By rainbow9 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: November 12th, 2011, 02:11 AM
  2. Simple beginner's password guessing program
    By edishuman in forum What's Wrong With My Code?
    Replies: 3
    Last Post: July 12th, 2011, 03:59 PM
  3. Password Program
    By computercoder in forum What's Wrong With My Code?
    Replies: 10
    Last Post: October 20th, 2010, 07:03 AM
  4. password
    By 5723 in forum Algorithms & Recursion
    Replies: 9
    Last Post: July 9th, 2009, 05:26 AM
  5. Problem in AWT and IFrame implementaion
    By AZBOY2000 in forum What's Wrong With My Code?
    Replies: 9
    Last Post: April 24th, 2009, 03:41 AM