Write an application that accepts a user\'s password from an input dialogs. When
ID: 3543725 • Letter: W
Question
Write an application that accepts a user's password from an input dialogs. When the entered password is less than six characters, more than 10 characters, or does not contain at least one letter and one digit, prompt the user again. When the user's entry meets all the password requirements, prompt the user to reenter the password, and do not let the user continue until the second password matches the first one. Save the file as Password.java.
I have 3 errors. HELP!!!
import javax.swing.*;
public class Password{
public static void main(String[] args){
String password = "";
String passwordRepeat = "";
boolean letterPresent = false;
boolean digitPressent = false;
char charChecked;
int len = password.length();
password = JOptionPane.showInputDialog("Enter your password:");
if((len < 6 || len > 10) || (password.isLetterOrDigit(password))) - there is an error here
{
password = JOptionPane.showInputDialog("Please keep password from 6 to 10 characters." +
" Enter your password:");
}
if((len < 6 || len > 10) && (password.isLetterOrDigit(password))) - there is an error here
{
passwordRepeat = showInputDialog("Please enter password again."); - there is an error here
}
}
}
Explanation / Answer
There were lot of things to be changed in the code. The more and more I went deeper in the code, more errors were found. I wrote one of my own. More like yours.
Code
import java.util.regex.Pattern;
import javax.swing.JOptionPane;
public class Password {
public static void main(String[] args) {
String password = JOptionPane.showInputDialog("Enter your password ");
boolean lessOrMoreCharacters = false, digit = false, letters = false;
if (password.length() > 10 || password.length() < 6) {
lessOrMoreCharacters = true;
}
if (!Pattern.compile("[0-9]").matcher(password).find()) {
digit = true;
}
if (!Pattern.compile("[a-zA-Z]").matcher(password).find()) {
letters = true;
}
if (lessOrMoreCharacters && digit && letters)
JOptionPane
.showMessageDialog(
null,
"Invalid password. Errors you committed. Password lenth should be in the range 6 to 10 characters. Password should have at least one digit Password should have at least one letter. ");
else if (lessOrMoreCharacters && digit)
JOptionPane
.showMessageDialog(
null,
"Invalid password. Errors you committed. Password lenth should be in the range 6 to 10 characters. Password should have at least one digit. ");
else if (lessOrMoreCharacters && letters)
JOptionPane
.showMessageDialog(
null,
"Invalid password. Errors you committed. Password lenth should be in the range 6 to 10 characters. Password should have at least one letter. ");
else if (letters && digit)
JOptionPane
.showMessageDialog(
null,
"Invalid password. Errors you committed. Password should have at least one digit. Password should have at least one letter. ");
else if (letters)
JOptionPane
.showMessageDialog(
null,
"Invalid password. Errors you committed. Password should have at least one letter. ");
else if (digit)
JOptionPane
.showMessageDialog(
null,
"Invalid password. Errors you committed. Password should have at least one digit. ");
else if (lessOrMoreCharacters)
JOptionPane
.showMessageDialog(
null,
"Invalid password. Errors you committed. Password lenth should be in the range 6 to 10 characters. ");
else{
String passwordTwo = JOptionPane.showInputDialog("Reenter your password ");
while (!passwordTwo.equals(password)){
passwordTwo = JOptionPane.showInputDialog("The passwords did not match. Reenter your password ");
}
JOptionPane.showMessageDialog(null, " It is a valid password ");
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.