When i run this it does not work like its supposed to. Basically the program is a password verifier that checks what the user enters. It works to check for the 6 characters but it does not work correctly to check for the uppercase, lowercase, or the digit.

import javax.swing.JOptionPane;


public class passwordVerifier {

	static String input;
	char ch;
	//prompt user for password
	
	
	public static void main(String[] args) {
		
	input = JOptionPane.showInputDialog("Please enter a 6 digit password that includes "
						+ "at least one uppercase letter, one lowercase letter "
						+ "and one numerical digit.");
	
	if(isValid(input)){
		JOptionPane.showMessageDialog(null, "That is a valid password.");
	}
	else{
		JOptionPane.showMessageDialog(null, "That is not a valid password.");
		
	}
	
	System.exit(0);
	}
	
	private static boolean isValid(String password){
		boolean valid = true;
		int i = 0;
		
		//verify password has 6 characters
		if (password.length()!= 6)
			valid = false;
		
				
		// verify that pasword contains at least one 
		// uppercase and one lowercase letter
		
		while (valid && i <6){
			if (!Character.isUpperCase(password.charAt(i)))
				valid = false;
			i++;
		}
		
		while (valid && i <=6){
			if (!Character.isLowerCase(password.charAt(i)))
				valid = false;
			i++;
		}
		
				
		// verify the password has one digit
		
		while (valid && i <=6){
			if (!Character.isDigit(password.charAt(i)))
				valid = false;
			i++;
		}
		
		return valid;
		
	}

}

Recommended Answers

All 4 Replies

it does not work correctly to check for the uppercase, lowercase, or the digit.

Could you show an example where the code does not work properly. Comment out the JOptionPane and just assign a value to the input variable that shows the problem.

Is the method supposed to show all the errors or can it fail on the first error found?
in other words, return failure on first find.

Try debugging the code by adding print outs to show each character as it is being tested and the results of each test.

i figured it out. basically it was failing at the point where i tested the uppercase. the first time it went through it went fine but the second time it returned false when it should have been true. im going to use a charr array to do it. should work i think. thx

char array vs charAt() won't fix your logic problem.
You need to play computer and go thru the logic of the loops step by step and see what happens to the variables.

char array did fix my problem. sry i didnt close this

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.