My code asks a user for a password. To properly validate the user's entry, it must include the following requirements:
1)have a length of 5
2)have an uppercase character
3)have a lowercase character
4)have a digit

My loop only works for the lowercase and length of 5 requirements. Can someone help me with validating uppercase and digit? Thanks in advance for any/all suggestions!

import javax.swing.*;

public class pwVer
{	
	public static void main(String[] args)
	{
		String input;  
		
		input = JOptionPane.showInputDialog("Please enter password");
		
		// Validate user's input
		if (isValid(input))
		{
			JOptionPane.showMessageDialog(null, "Your password is valid!");
		}
		else
		{
			JOptionPane.showMessageDialog(null, "Your password is invalid " +
					"valid password criteria.");
		}
		System.exit(0);
		}
		/**
			The isValid method determines password validity.
			@param passWord The String to test.
			@return true if valid, or else false.
		*/
		
		private static boolean isValid(String pw)
		{
		boolean isvalid = true;
		int i = 0;
			
		//Test the length
		if (pw.length() != 5)
			isvalid = false;
		
		//Test for lowercase
		while (isvalid && i < 5)
		{
			if (!Character.isLowerCase(pw.charAt(i)))
			isvalid = false;
			i++;
		}
		
		//Test for uppercase
		while (isvalid && i < 5)
		{
			if (!Character.isUpperCase(pw.charAt(i)))
				isvalid = false;
			i++;
		}
		
		//Test for digit
		while (isvalid && i < 5)
		{
			if (!Character.isDigit(pw.charAt(i)))
				isvalid = false;
			i++;
		}

		return isvalid;
	}
}

The lowercase part you have actually won't work for all cases...

What I would do is just have a boolean variable for each condition, and each check will set the appropriate variable to true or false. Then at the end just 'and' them all together to get whether it is valid.

So for lowercase, have the isLowercase variable set to false, then loop through and once you find a lowercase character, set it to true. Same goes for uppercase and digit.

Wow and thx! - that was a quick response.

But, I'm confused. Can you show me with one of the validations?

You been on right track, but you just forgot to set counter after each while loop back to zero. Because of that capital letters and digit is never checked.
Also you may consider something like this

import javax.swing.*;

public class PasswordCheck
{	
	public static void main(String[] args)
	{
		String input;  
		
		input = JOptionPane.showInputDialog("Please enter password");
		
		// Validate user's input
		if (isValid(input))
		{
			JOptionPane.showMessageDialog(null, "Your password is valid!");
		}
		else
		{
			JOptionPane.showMessageDialog(null, "Your password is invalid " +
					"valid password criteria.");
		}
		System.exit(0);
	}
		
		/**
			The isValid method determines password validity.
			@param passWord The String to test.
			@return true if valid, or else false.
		*/
		
	private static boolean isValid(String pw)
	{
		if(checkLength(pw) && lowCaseCheck(pw) && upCaseCheck(pw) && digCheck(pw))
		{
			return true;
		}
		return false;
	}
	
	private static boolean checkLength(String psw)
	{
		if (psw.length() == 5){return true;}
		return false;
	}
	
	private static boolean lowCaseCheck(String psw)
	{
		for(int i = 0; i < psw.length(); i++)
		{
			if(Character.isLowerCase(psw.charAt(i)))
			{
				return true;
			}
		}
		return false;
	}
	
	private static boolean upCaseCheck(String psw)
	{
		for(int i = 0; i < psw.length(); i++)
		{
			if(Character.isUpperCase(psw.charAt(i)))
			{
				return true;
			}
		}
		return false;
	}
	
	private static boolean digCheck(String psw)
	{
		for(int i = 0; i < psw.length(); i++)
		{
			if(Character.isDigit(psw.charAt(i)))
			{
				return true;
			}
		}
		return false;
	}
}

You been on right track, but you just forgot to set counter after each while loop back to zero. Because of that capital letters and digit is never checked.
Also you may consider something like this

import javax.swing.*;

public class PasswordCheck
{	
	public static void main(String[] args)
	{
		String input;  
		
		input = JOptionPane.showInputDialog("Please enter password");
		
		// Validate user's input
		if (isValid(input))
		{
			JOptionPane.showMessageDialog(null, "Your password is valid!");
		}
		else
		{
			JOptionPane.showMessageDialog(null, "Your password is invalid " +
					"valid password criteria.");
		}
		System.exit(0);
	}
		
		/**
			The isValid method determines password validity.
			@param passWord The String to test.
			@return true if valid, or else false.
		*/
		
	private static boolean isValid(String pw)
	{
		if(checkLength(pw) && lowCaseCheck(pw) && upCaseCheck(pw) && digCheck(pw))
		{
			return true;
		}
		return false;
	}
	
	private static boolean checkLength(String psw)
	{
		if (psw.length() == 5){return true;}
		return false;
	}
	
	private static boolean lowCaseCheck(String psw)
	{
		for(int i = 0; i < psw.length(); i++)
		{
			if(Character.isLowerCase(psw.charAt(i)))
			{
				return true;
			}
		}
		return false;
	}
	
	private static boolean upCaseCheck(String psw)
	{
		for(int i = 0; i < psw.length(); i++)
		{
			if(Character.isUpperCase(psw.charAt(i)))
			{
				return true;
			}
		}
		return false;
	}
	
	private static boolean digCheck(String psw)
	{
		for(int i = 0; i < psw.length(); i++)
		{
			if(Character.isDigit(psw.charAt(i)))
			{
				return true;
			}
		}
		return false;
	}
}

Thank you so much Peter_budo! You've clarified alot for me. It never fails, but I get so far with Java and then there's always 1 or 2 things that I don't understand.

Again, thank you for taking the time. It is truly appreciated.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.