For an assignment, I am writing a password program in java. I need someone to he
ID: 3656097 • Letter: F
Question
For an assignment, I am writing a password program in java. I need someone to help me with my code. For some reason while(!validPWord) does not run. When I run the program it asks for a password and then immediately asks the user to reenter the password regardless of user input. //password needs to be between 6 and 10 characters //must contain 1 letter and 1 number //must be confirmed once a valid password has been entered import javax.swing.*; public class PasswordChecker { public static void main(String[] args) { boolean validPWord = true; String pWord = ""; String reenterPWord = ""; char pChar = ' ' ; boolean hasDigit = false; boolean hasLetter = false; int count = 0; boolean pWordsMatch = false; pWord = JOptionPane.showInputDialog(null,"Enter your chosen password."); while (!validPWord) { while ((hasDigit == false) && (count < pWord.length())) { pChar = pWord.charAt(count); hasDigit = Character.isDigit(pChar); count++; } count = 0; while ((hasLetter == false)&&(count < pWord.length())) { pChar = pWord.charAt(count); hasLetter = Character.isLetter(pChar); count++; } if((pWord.length() < 6) || (pWord.length() > 10)) { JOptionPane.showMessageDialog(null,"The password is the wrong length."); hasDigit = false; hasLetter = false; } else if (!hasDigit) { JOptionPane.showMessageDialog(null, "Password does not contain a number."); hasDigit = false; hasLetter = false; } else if (!hasLetter) { JOptionPane.showMessageDialog(null, "Password does not contain a letter."); hasDigit = false; hasLetter = false; } else { validPWord = true; } } reenterPWord = JOptionPane.showInputDialog(null,"Reenter your password."); while(pWordsMatch == false) { if(reenterPWord.equals(pWord)) { JOptionPane.showMessageDialog(null,"That's a nice password, you'll need it."); } else { JOptionPane.showMessageDialog(null, "Wrong! Enter it again."); reenterPWord = JOptionPane.showInputDialog(null,"Reenter your password."); } } System.exit(0); } }Explanation / Answer
import java.util.*;import java.lang.String;import java.lang.Character;public class CheckingPassword { public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.print("Please enter a Password: "); String password = input.next(); if (isValid(password)) { System.out.println("Valid Password"); } else { System.out.println("Invalid Password"); } } public static boolean isValid(String password) { if (password.length() < 8) { return false; } else { char c; int count = 0; for (int i = 0; iRelated Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.