Error Loop for Confirmation


#1

I need this program to re-prompt the user whenever the user does not put in the needed input. So far it can either accept the user input and display a message that confirms the correct input, and or only lets the user know that they didnt put in the right input.
My program is supposed to keep asking the user for the correct input until they get it right. Here is my code so far:

import javax.swing.JOptionPane;
public class ValidatePassword {

public static void main(String[] args) {
	// TODO Auto-generated method stub
	String userPw;
	String checkPw;
	char e = 0;
	boolean capital = false;
	boolean lower = false;
	boolean num = false;

	
	userPw = JOptionPane.showInputDialog(null, "Please enter a password" + 
	" that contains the following:" + "\n"
	+ "**TWO upper case letters" + "\n" + "**THREE lower case letters" + "\n"
	+ "**ONE number");
	checkPw = userPw;
	
	
	for(int i = 0; i < checkPw.length(); i++) {
		e = checkPw.charAt(i);
		
		if(Character.isDigit(e)) {
			num = true;
		}else if(Character.isUpperCase(e)){
			capital = true;
		}else if(Character.isLowerCase(e)) {
			lower = true;
		} 
	} 
	
		if(num && capital && lower) {
			JOptionPane.showMessageDialog(null, "Password requirements met: " +checkPw+ 
			" is your new password.");
		}else{ 
			
			JOptionPane.showInputDialog(null, "The password you've entered: " +"-"+checkPw+"-"+ 
			" Has not met requirements." + "\n" 
			+ "Please make sure that your password renter a password that contains the following: " + "\n"
			+ "**TWO upper case letters" + "\n" + "**THREE lower case letters" + "\n"
			+ "**ONE number");
		}
	}
}

Please help. Thank you for your time!


#2

For this we can use functions and the while loop.

The while loop repeats itself untill a certain check becomes true/false.
for example:

playing = true
while(playing){
  refresh screen;
  if(exit is pressed){
    playing = false;   // exit the while loop
  }
}

Note: It is important to exit a while loop. Otherwise you will get yourself an infinite loop which will lock your program.

A function is a peace of code that can be called again and again.
because you want to check the password over and over again (until the user provides a correct password). We can put this check in a function. This way we can call it every time the user provides a new password that has to be checked.

Here is your code with the while loop and the function build in. I am not able to test the code so you might need to do some debugging.

import javax.swing.JOptionPane;
public class ValidatePassword {
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		String userPw;
		
		userPw = JOptionPane.showInputDialog(null, "Please enter a password" + 
		" that contains the following:" + "\n"
		+ "**TWO upper case letters" + "\n" + "**THREE lower case letters" + "\n"
		+ "**ONE number");
		
		while(!isCorrectPassword(userPw)){ // as long as user provides wrong password ask again.
			userPw = JOptionPane.showInputDialog(null, "The password you've entered: " +"-"+checkPw+"-"+ // not a good idea to show password to user.
			" Has not met requirements." + "\n" 
			+ "Please make sure that your password renter a password that contains the following: " + "\n"
			+ "**TWO upper case letters" + "\n" + "**THREE lower case letters" + "\n"
			+ "**ONE number");
		}
		if(isCorrectPassword(userPw)) {	// if user provides correct password show succes
			JOptionPane.showMessageDialog(null, "Password requirements met: " +checkPw+ 
			" is your new password.");
		}
	}
	// check if given password are true in specified requirements.
	private boolean isCorrectPassword(string password){
		char e = 0;
		boolean capital = false;
		boolean lower = false;
		boolean num = false;
		for(int i = 0; i < checkPw.length(); i++) {
			e = checkPw.charAt(i);
			
			if(Character.isDigit(e)) {
				num = true;
			}else if(Character.isUpperCase(e)){
				capital = true;
			}else if(Character.isLowerCase(e)) {
				lower = true;
			} 
		}
		return capital && lower && num // returns true if all variables = true. otherwise return false.
	}
}

I have commented alot of what i did. If there are things still unclear please ask.

Note: You are showing the password that the user provided in the messagebox. This is usually not a good idea. This password might be used by the user on other services and people might be looking over the users shoulder.