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

Thread: help with this problem

  1. #1
    Junior Member
    Join Date
    Nov 2020
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default help with this problem

    I'm trying to create a password verification routine, my problem is when a input is entered it will go through each if statement individually. I'm trying to restart validation when a condition is not met. At the moment it only jumps to the next if statement when the previous condition is met, even though its a new input. Thanks in advance.
    import java.util.Scanner;
     
    public class passWordCheck {
     
    	public static void main(String[] args) {
     
    		String passWord; // Initialize string for the password.
     
    		Scanner code = new Scanner(System.in);
    		// Give requirements for a valid password.
    		System.out.println("1. A code must contain no spaces");
    		System.out.println("2. A code must have between 5 and 10 characters (inclusive)");
    		System.out.println("3. A code must begin with \\ and must end with \\");
    		System.out.println("4. A code must contain at least one lowercase letter and at least one uppercase letter");
    		System.out.print("Please enter a code: ");
    		passWord = code.nextLine();
    		// Do While loop to check all requirement for a valid password are met.
    		do {
     
    			if (!firstCharacter(passWord)) {// Check First Character
    				System.out.println("Code must start with \\.");
    				System.out.println("Code is invalid, Please try again");
    				passWord = code.nextLine();
    			}
    			if (!lastCharacter(passWord)) {// Check last Character
    				System.out.println("Code must end with \\.");
    				System.out.println("Code is invalid, Please try again");
    				passWord = code.nextLine();
    			}
    			if (!hasCorrectLength(passWord)) {// Check password length
    				System.out.println("Code must be between 5 and 15 characters in lenth.");
    				System.out.println("Code is invalid, Please try again");
    				passWord = code.nextLine();
    			}
    			if (!hasUpperCharacter(passWord)) {// Check for uppercase Character
    				System.out.println("Code must contain at least one uppercase letter.");
    				System.out.println("Code is invalid, Please try again");
    				passWord = code.nextLine();
    			}
    			if (!hasLowerCharacter(passWord)) {// Check for lowercase character
    				System.out.println("Code must contain at least one lowercase letter.");
    				System.out.println("Code is invalid, Please try again");
    				passWord = code.nextLine();
    			}
     
    		} while (false);
     
    		System.out.println("code is valid.");
    		code.close();
     
    	}
     
    	// Methods for checking password requirements
     
    	public static boolean firstCharacter(String str) {
     
    		char first = str.charAt(0); // First character of a string
     
    		if (first == ('\\')) // This verifies there is a \ at the start.
    		{
    			return true;
    		}
    		return false;
     
    	}
     
    	public static boolean lastCharacter(String str) {
     
    		int n = str.length(); // Finding string length
     
    		char last = str.charAt(n - 1); // Last character of a string
     
    		if (last == ('\\')) // This verifies there is a \ at the end.
    		{
    			return true;
    		}
    		return false;
     
    	}
     
    	public static boolean hasCorrectLength(String str) {
     
    		int n = str.length(); // Finding string length
     
    		if (n >= 5 || n <= 15) // This verifies the correct length.
    		{
    			return true;
    		}
     
    		return false;
    	}
     
    	public static boolean hasLowerCharacter(String str) {
     
    		for (int i = 0; i < str.length(); i++) { // For loop to check password for lowercase
    			char currentCharacter = str.charAt(i);
    			if (Character.isLowerCase(currentCharacter)) {
    				return true;
    			}
    		}
    		return false;
     
    	}
     
    	public static boolean hasUpperCharacter(String str) {
     
    		for (int i = 0; i < str.length(); i++) { // For loop to check password for uppercase
    			char currentCharacter = str.charAt(i);
    			if (Character.isUpperCase(currentCharacter)) {
    				return true;
    			}
     
    		}
    		return false;
     
    	}
     
    }
    Last edited by drnick384; November 2nd, 2020 at 06:51 AM.

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: help with this problem

    Please copy the contents of the command prompt window and paste it here that shows what you are asking about. Add some comments to the output to describe where the output show change.

    Please edit your post and wrap your code with code tags:

    [code]
    **YOUR CODE GOES HERE**
    [/code]

    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Nov 2020
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: help with this problem

    Thanks for the heads up. I'm basically trying to get the loop to re run when 1 of the conditions is not met. At the moment it loops through individually. I tried the multiple next.() to try get back to the start of the verification. Line 46 is also an attempt to re run the if statements when one if the conditions are false.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: help with this problem

    Can you post the contents of the command prompt window from when you execute the program that shows the problem?

    Line 46
    Which line is line 46? The lines are not numbered in the display.

    		} while (false);  // never loops if false
    Why the condition of false? That means it is not supposed to loop
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Nov 2020
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: help with this problem

    Sorry line 46 is the While(false). its so the loop will re run when conditions are false. My problem is that when the password is entered the conditions are checked when the the first condition is correct but the others are not and another input is required the routine starts the check from where it left off and not the start of the if statements.
    [img][/img]

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: help with this problem

    while(false) will never loop.

    If you want to control when the code loops and when it exits the loop, change the while statement to use a variable's contents to control the looping.
    For example if the value of keepLooping is true, the code will loop. If the value of keepLooping is false, the loop will exit.
    Change the value in keepLooping as required for when you want the loop to exit or not
    keepLooping is defined as a boolean variable that can hold true or false.
      while(keepLooping);  // loop if keepLooping is true; exit if it is false.
    If you don't understand my answer, don't ignore it, ask a question.

  7. The Following User Says Thank You to Norm For This Useful Post:

    drnick384 (November 2nd, 2020)

Similar Threads

  1. Catch block problem. Please fix my problem.
    By damnitsme in forum What's Wrong With My Code?
    Replies: 3
    Last Post: February 3rd, 2014, 01:49 AM
  2. Problem with Project Euler problem 18
    By sara_magdy in forum What's Wrong With My Code?
    Replies: 2
    Last Post: July 19th, 2013, 12:46 PM
  3. Replies: 3
    Last Post: January 5th, 2012, 01:44 AM
  4. [SOLVED] [Problem] imports javax.swing problem
    By Brollie in forum AWT / Java Swing
    Replies: 8
    Last Post: July 5th, 2009, 07:59 AM
  5. Java program for 2-D Array Maze
    By Peetah05 in forum Collections and Generics
    Replies: 11
    Last Post: May 8th, 2009, 04:30 AM