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