Question

In: Computer Science

I need a basic and simple Java code for this exercise: (Check password) Some websites impose...

I need a basic and simple Java code for this exercise:

(Check password) Some websites impose certain rules for passwords. Write a method that checks whether a string is a valid password. Suppose the password rules are as follows:

■ A password must have at least eight characters. A character is alpha or digit.

■ A password consists of only letters and digits. No other symbols.

■ A password must contain at least two digits.

Write a program that prompts the user to enter a password and displays Valid Password if the rules are followed or Invalid Password otherwise. For example, “abcdef12” is legal, “12345678” is legal, “$abcde12” is not legal, “abcdefgh” is not legal.

Solutions

Expert Solution

CheckingPassword.java


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) {

//return true if and only if password:

//1. have at least eight characters.
//2. consists of only letters and digits.
//3. must contain at least two digits.

if (password.length() < 8) {   
return false;
} else {
char c;
int count = 0;   
for (int i = 0; i < password.length(); i++) {
c = password.charAt(i);
if (!Character.isLetterOrDigit(c)) {
return false;
} else if (Character.isDigit(c)) {
count++;   
}
}
if (count < 2) {   
return false;
}
}
return true;
}
}

OUTPUT:


Related Solutions

Password check, lab2pr1.py Many websites these days require that a password is between 8 and 20...
Password check, lab2pr1.py Many websites these days require that a password is between 8 and 20 characters, doesn’t have spaces, and satisfies the following conditions: contain at least one uppercase letter, contain at least lowercase letter, contain at least one number, and contain at least one special symbol character (”!?,.;:$# &”). Write a program that keeps asking the user to enter the password until they enter one that satisfies the requirements. Please enter a password: @Tulane2020 Please enter a password:...
I need a java code Write a simple program to prompt the user for a number...
I need a java code Write a simple program to prompt the user for a number between 1 and 12 (inclusive) and print out the corresponding month. For example:   The 12th month is December. Use a java "switch" statement to convert from the number (1-12) to the month. Also use a "do while" loop and conditional checking to validate that the number entered is between 1 and 12 inclusive and if not, prompt the user again until getting the correct...
Write a Java method to check whether a string is a valid password. Password rules: A...
Write a Java method to check whether a string is a valid password. Password rules: A password must have at least ten characters. A password consists of only letters and digits. A password must contain at least two digits. There are at least SIX functions/methods as the following: 1. menu() – to print the password’s rules. 2. getString() – to get the password from the user. 3. isPassword() – to check either the password is valid based on the given...
I need a full java code. And I need it in GUI With the mathematics you...
I need a full java code. And I need it in GUI With the mathematics you have studied so far in your education you have worked with polynomials. Polynomials are used to describe curves of various types; people use them in the real world to graph curves. For example, roller coaster designers may use polynomials to describe the curves in their rides. Polynomials appear in many areas of mathematics and science. Write a program which finds an approximate solution to...
JAVA JAVA JAVA Hey i need to find a java code for my homework, this is...
JAVA JAVA JAVA Hey i need to find a java code for my homework, this is my first java homework so for you i don't think it will be hard for you. (basic stuff) the problem: Write a complete Java program The transport Company in which you are the engineer responsible of operations for the optimization of the autonomous transport of liquid bulk goods, got a design contract for an automated intelligent transport management system that are autonomous trucks which...
Java homework problem: I need the code to be able to have a message if I...
Java homework problem: I need the code to be able to have a message if I type in a letter instead of a number. For example, " Please input only numbers". Then, I should be able to go back and type a number. import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JTextField; public class LoginGui {    static JFrame frame = new JFrame("JFrame Example");    public static void main(String s[]) {        JPanel panel...
2. Write a Java program to read a string (a password)from the user and then   check...
2. Write a Java program to read a string (a password)from the user and then   check that the password conforms to the corporate password policy.   The policy is:   1) the password must be at least 8 characters   2) the password must contain at least two upper case letters   3) the password must contain at least one digit   4) the password cannot begin with a digit   Use a for loop to step through the string.   Output “Password OK” if the password...
I need an idea of Java code that will convert an integer (1 to 3,999) into...
I need an idea of Java code that will convert an integer (1 to 3,999) into roman numerals using if statements; arrays and loops sadly aren't allowed and that's all I can come up with.
(This is for java) I need to rewrite this code that uses a while loop. public...
(This is for java) I need to rewrite this code that uses a while loop. public class Practice6 {      public static void main (String [] args) {         int sum = 2, i=2;        do { sum *= 6;    i++;    } while (i < 20); System.out.println("Total is: " + sum); }
I need to make this into a Java Code: Write a program that prompts the user...
I need to make this into a Java Code: Write a program that prompts the user for a double value representing a radius. You will use the radius to calculate: circleCircumference = 2πr circleArea = πr2 sphereArea = 4πr2 sphereVolume = 43πr3 You must use π as defined in the java.lang.Math class. Your prompt to the user to enter the number of days must be: Enter radius: Your output must be of the format: Circle Circumference = circleCircumference Circle Area...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT