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

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 am in beginners java course so using the most simple/basic JAVA please complete the following...
I am in beginners java course so using the most simple/basic JAVA please complete the following CODE SEGMENTS. (2 parts) FIRST PART: Using ITERATIVE create a METHOD MAX that returns the max element in an ArrayList of ints and prints all of the elements. *you have to use an iterative in the method.* (just need to write the METHOD ONLY not a whole program(will do in 2nd part), assume you are given any numbers/integers. SECOND PART: Now write a class...
•Write a JAVA program to check a given password strength from a user's input. •Create a...
•Write a JAVA program to check a given password strength from a user's input. •Create a method to check the number of characters. It must be more than 8. •Create a method to check the password to have at least one uppercase letter. •Create a method to check the password to have at least one lowercase letter. •Create a method to check the password to have at least one digit. •Create a method to check the password to have at...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT