|
-
Apr 8th, 2010, 06:59 PM
#1
Thread Starter
Member
Quick Java Question
I wrote a program that works well, but not perfectly. The program asks the user to enter a password then checks the password to make sure it is between 6 and 10 characters in length, and contains at least 1 letter and 1 number. If the password meets those requirements, the user is alerted that the password is "good". If it does not meet the requirements, the user is alerted that the password is no good, and they need to try again. All of this works fine, but I need the program to KEEP asking for a password until one is given that meets the requirements. Right now, it only asks one time, and gives the result.
Also, once a password meets the requirements, the user is to type it again to verify that it matches the original password typed. This works fine as well, but it also needs to keep asking until the passwords match.
Can anyone help me out on this? I am not sure the best way to do this. I am new to Java, and trying to teach myself with a book I have purchased. Thanks.
Here is the code:
Code:
import java.util.Scanner;
public class PasswordChecker
{
// Create main method
public static void main(String[] args)
{
// Declare variables
Scanner sc = new Scanner(System.in);
int letters = 0;
int digits = 0;
// Prompt user to enter a possible password
System.out.println("Please enter a possible password.");
String password = sc.nextLine();
int len = password.length(); // Determines the lenght of the password
// Check to see if the password contains at least one number and one letter
for (int i=0;i<password.length();++i)
{
char c=password.charAt(i);
if (Character.isLetter(c)) ++letters;
if (Character.isDigit(c)) ++digits;
}
// Check to see that the password is between 6 and 10 characters long, and contains 1 letter and 1 number, minimum
// If the password meets the criteria, alert user. If not, ask user to enter a new password
if (len < 6 || len > 10 || letters == 0 || digits == 0)
System.out.println("Passwords must be between 6 and 10 characters in lenght, and contain at least 1 letter, and 1 number. Please try again.");
else
System.out.println("This is a good password. Please re-enter your password for verification.");
// If the password is "good", compare the 2 passwords to see if they match, and report results to the user
String password2 = sc.nextLine();
if (password.equals(password2))
System.out.println("Congratulations, the passwords match.");
else
System.out.println("The passwords do not match. Please try again");
}
}
-
Apr 9th, 2010, 04:31 PM
#2
Re: Quick Java Question
Code:
import java.util.Scanner;
public class PasswordChecker
{
// Create main method
public static void main(String[] args)
{
// Declare variables
Scanner sc = new Scanner(System.in);
int letters = 0;
int digits = 0;
boolean goodPassword;
do
{
// Prompt user to enter a possible password
System.out.println("Please enter a possible password.");
String password = sc.nextLine();
int len = password.length(); // Determines the lenght of the password
// Check to see if the password contains at least one number and one letter
for (int i = 0; i < password.length(); ++i)
{
char c = password.charAt(i);
if (Character.isLetter(c))
++letters;
if (Character.isDigit(c))
++digits;
}
goodPassword = !(len < 6 || len > 10 || letters == 0 || digits == 0);
// Check to see that the password is between 6 and 10 characters long, and contains 1 letter and 1 number, minimum
// If the password meets the criteria, alert user. If not, ask user to enter a new password
if (len < 6 || len > 10 || letters == 0 || digits == 0)
System.out.println("Passwords must be between 6 and 10 characters in lenght, and contain at least 1 letter, and 1 number. Please try again.");
else
System.out.println("This is a good password. Please re-enter your password for verification.");
}
while (!goodPassword);
}
private PasswordChecker()
{
}
}
"I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
My Blog
-
Apr 10th, 2010, 03:51 PM
#3
Thread Starter
Member
Re: Quick Java Question
ok, I just re-read the lesson in the book, and it had more requirements for this program, so now I had to change a lot. I am stuck now. The program does not work perfectly yet.
NOTE: As it is now, I wrote it just to test that the code written so far works, I will add the rest of the required code after I know this works fine.
Program Requirements:
Main Method
-Sets up a string variable
-Calls the getPassword method
-Prints out the password string to demonstrate it was required properly
getPassword() Method
- Loop unitl a good password is entered TWICE
- Calls the isGoodPassowrd() method to determine that the password met its requirements within that method
isGoodPassword() Method
-Accepts a String and returns a Boolean
CODE:
Code:
import java.util.Scanner;
// Create Password Class
public class Password
{
// Create main method
public static void main(String[] args)
{
// Declare variables
Scanner sc = new Scanner(System.in);
String password = sc.nextLine();
// Call isGoodPassword() method
isGoodPassword(password);
}
// Create isGoodPassword Method
public static void isGoodPassword(String password)
{
// Declare variables
int letters = 0;
int digits = 0;
boolean goodPassword;
int len = password.length(); // Determines the lenght of the password
// Loop that prompts user to enter a password until one is entered that meets the criteria
do
{
// Prompt user to enter a possible password
System.out.println("Please enter a password");
// Check to see if the password contains at least one number and one letter
for (int i = 0; i < password.length(); ++i)
{
char c = password.charAt(i);
if (Character.isLetter(c))
++letters;
if (Character.isDigit(c))
++digits;
}
goodPassword = !(len < 6 || len > 10 || letters == 0 || digits == 0);
// Check to see that the password is between 6 and 10 characters long, and contains 1 letter and 1 number, minimum
// If the password meets the criteria, alert user. If not, ask user to enter a new password
if (len < 6 || len > 10 || letters == 0 || digits == 0)
System.out.println("Passwords must be between 6 and 10 characters in lenght, and contain at least 1 letter, and 1 number. Please try again.");
else
System.out.println("This is a good password. Please re-enter your password for verification.");
}
while (!goodPassword);
}
}
Last edited by CarlMartin10; Apr 10th, 2010 at 04:15 PM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|