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

Thread: New guy having problems with for loops, methods and charAt

  1. #1
    Junior Member
    Join Date
    Sep 2013
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default New guy having problems with for loops, methods and charAt

    Hello,
    I'm new to programming and am having a problem with my homework assignment. Some parts will work but I can't figure out how I've used some loops, methods and such incorrectly. The for loop and the if statements that contain matches are not working and I am at a loss as to why. Any help would be greatly appreciated!

    import java.util.Scanner;
     
    public class PasswordVerifier 
    {
     
    	public static void main(String[] args) 
    	{
    		Scanner stdIn = new Scanner(System.in);
    		String newPassword;		//user input
     
    		boolean legalPassword = true;	// is the user input legal?
    		boolean rightLength = true;		//determines correct length
            boolean uCase = true;			// Has upper case
            boolean lCase = true;			// has lower case
            boolean sCase = true;			// has special character
            boolean digit = true;			// has a number
            boolean noContainCase = true;	// doesn't have and or end
     
    //newPassword print statement
     
    		System.out.print("Password Verifier\n");
    		System.out.print("\nEnter a password that meets the following rules: \n\n" +
    		"\tIs at least 8 characters long\n" +
    		"\tContains at least 1 upper case letter\n" +
    		"\tContains at least 1 lower case letter\n" +
    		"\tContains at least 1 numeric digit\n" +
    		"\tContains at least 1 of the following: !@#$%^&*\n" +
    		"\tDoes not contain the words \"and\" or \"end\"\n" );
    		newPassword = stdIn.nextLine();	// user input
     
    // newPassword conditions
     
    		if (newPassword.length() < 8)	
    		{
    			rightLength = false; 
    			legalPassword = false;
    		}
     
    		for (int i = 0; i < newPassword.length(); i++)
    		{
    			if (!(Character.isUpperCase(newPassword.charAt(i))));
    				{
    					uCase = false;
    					legalPassword = false;
    					i++;
    				}
    			if (!(Character.isLowerCase(newPassword.charAt(i))));
    				{
    					lCase = false;
    					legalPassword = false;
    					i++;
    				}
    			if (!(Character.isDigit(newPassword.charAt(i))));
    				{
    					digit = false;
    					legalPassword = false;
    					i++;
    				}
    		}
     
    		String specialCh = "(.*!,@,#,$,%,^,&,*.*)";
    		if (!(newPassword.matches(specialCh)))
    		{
    			sCase = false;
    			legalPassword = false;
    		}
     
    		String noContain = "(.*and, end.*)";
    		if (!(newPassword.matches(noContain)))
    		{
    			noContainCase = false;
    			legalPassword = false;
    		}
     
    		if (legalPassword)
    		{
    				System.out.print("Valid");
    		}
     
    		else if (legalPassword != true)
    		{
    			System.out.print("Invalid\n");
    		}
     
    // Print statements		
    		if (rightLength != true)
    		{
    			System.out.print("Password must be at least 8 characters long\n");
    		}
     
    		if (uCase != true)
    		{
    			System.out.print("Must contain an upper case letter\n");
    		}
     
    		if (lCase != true)
    		{
    			System.out.print("Must contain a lower case letter\n");
    		}
     
    		if (digit != true)
    		{
    			System.out.print("Must contain a digit\n");
    		}
     
    		if (sCase != true)
    		{
    			System.out.print("Must contain a special character from the list\n");
    		}
     
    		if (noContainCase != true)
    		{
    			System.out.print("Must not contain \"and\" or \"end\"");
    		}
    	}
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: New guy having problems with for loops, methods and charAt

    I think one of your problems is with the regex you are using, not the if statements or charAt() method.

    For testing the specific methods, write a small program that has the input hard coded and calls the method being tested:
      char c = 'A';
      System.out.println(Character.isUpperCase(c));
    Change the value of c for different cases. Compile and execute to see the results
    Last edited by jps; September 22nd, 2013 at 10:40 AM. Reason: fixed one too many (s
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: New guy having problems with for loops, methods and charAt

    What does "are not working" mean?
    If there is an error message, copy-paste it to the forum

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: New guy having problems with for loops, methods and charAt

    Oops. One too many (s inside println()
    If you don't understand my answer, don't ignore it, ask a question.

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

    Default Re: New guy having problems with for loops, methods and charAt

    I don't get an error code. This is what prints when I make an input such as paswrd1. It doesn't matter what it contains as far as the special character, lower case, upper case, etc. The only time I can get the valid password response is if it is longer than 8 characters. Otherwise I get not response either for valid or invalid.

    Password Verifier

    Enter a password that meets the following rules:

    Is at least 8 characters long
    Contains at least 1 upper case letter
    Contains at least 1 lower case letter
    Contains at least 1 numeric digit
    Contains at least 1 of the following: !@#$%^&*
    Does not contain the words "and" or "end"
    paswrd1
    Invalid
    Password must be at least 8 characters long
    Must contain an upper case letter
    Must contain a lower case letter
    Must contain a digit
    Must contain a special character from the list
    Must not contain "and" or "end"

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: New guy having problems with for loops, methods and charAt

    You need to try some debugging to see where the problems are. I use the println() statement to print out the values of variables as they are changed and the values returned by each method that is called.
    The print out will show you where the code is doing something different from what you want.

    You need to find which of the many tests the code makes is not returning the right value. When you find that, you can focus on fixing the test so it does what you want.

    As I suggested in post#2, you should write a short simple program that does each test in a few statements and prints out the result. Then you will find which tests you are using are not working as desired.

    --- Update ---

    My compiler gives warnings for the code when I use the -Xlint option. You should use that option with the javac compiler so you will see the warnings. Here is the commandline I use to compile:
    javac.exe -cp . -Xlint PasswordVerifier.java
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    Sep 2013
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: New guy having problems with for loops, methods and charAt

    Thank you. I am working on doing that right now.

Similar Threads

  1. Tic-Tac-Toe (loops, arrays, and methods) help please
    By CVL220 in forum Collections and Generics
    Replies: 3
    Last Post: November 10th, 2012, 01:47 PM
  2. If statement: ==/.equals() problems with .charAt()
    By Omnamah in forum Loops & Control Statements
    Replies: 3
    Last Post: June 29th, 2012, 12:39 PM
  3. Need Help with Writing methods that involves Loops
    By jslam in forum Loops & Control Statements
    Replies: 7
    Last Post: February 27th, 2012, 01:29 AM
  4. Using charAt and other string methods in a created class
    By briankfmn in forum Object Oriented Programming
    Replies: 1
    Last Post: February 8th, 2012, 01:54 AM
  5. How to compile and run java program?
    By ebalari56 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: September 27th, 2009, 09:33 PM