Attempting to create a Java password program?
-
Java program that asks for user input of a password that must be between 6 and 10 characters and must contain at least one digit and one letter. So far I am having problems remember how to check if a digit or char is present and struggling with my while loops to continue if the users passwords don't match any help will be greatly appreciated. import java.util.Scanner; public class PasswordCreator{ public static void main(String[]args){ Scanner input = new Scanner(System.in); int digit = 0; int letter = 0; String password,verifyPassword; System.out.println("Please enter an password"); password=input.nextLine(); while(password.length()<6 || password.length()>10) { System.out.println("Password must be between 6 and 10 characters please re-enter"); password=input.nextLine(); } System.out.println("Password you entered in valid please re-enter to confirm"); verifyPassword=input.nextLine(); while(verifyPassword.equals(password… { System.out.println("Your passwords match Thank You!"); } } }
-
Answer:
first of all change while(verifyPassword.equals(password)) { System.out.println("Your passwords match Thank You!"); } to while( ! verifyPassword.equals(password) ) { System.out.println("Passwords not matching. please re-enter"); verifyPassword=input.nextLine(); } System.out.println("Your passwords match Thank You!"); Now.. checking for presence of a char or digit. better earn and use regular expressions. ok..i will put a trail code here.. ok add this statement to your program String pattern = "(\\w+)((\\d+)(\\w+))"; now modify while(password.length()<6 || password.length()>10) to while(password.length()<6 || password.length()>10 || !password.matches(pattern)) now your program will accept passwords containing alphabets and digits only
Lucas at Yahoo! Answers Visit the source
Related Q & A:
- how to Create a Java Package from MATLAB Code?Best solution by Stack Overflow
- how to create a new syntax in java?Best solution by Stack Overflow
- How to create a java applet?Best solution by Stack Overflow
- How to create a simple counter program?Best solution by Stack Overflow
- How to create this Java program?Best solution by ChaCha
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.