How do I code password varifacation

I have a password program but I need it to verify the password entered

Page 1 of 1

5 Replies - 3418 Views - Last Post: 19 April 2010 - 07:56 PM Rate Topic: -----

#1 boosh  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 57
  • Joined: 14-January 10

How do I code password varifacation

Posted 13 April 2010 - 08:32 PM

First off I would like to thank all the folks who have spent time to help me out. THANKS GUYS :bananaman:.

My issue is this I have writen a password program but I need it to verify the password after it is entered correctly. heres the code I have
import java.util.Scanner;

public class Password
{
	// 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 6 to 10 characters long, and contains 1 letter and 1 number.
			if (len < 6 || len > 10 || letters == 0 || digits == 0)
				System.out.println("Passwords must be 6 to 10 characters in lenght. Contain at least 1 letter, and 1 number. Please try again.");
			else
				System.out.println("Excepted. Please re-enter your password for verification.");
		}
		while (!goodPassword);
	}

	private Password()
	{
	}
}


Is This A Good Question/Topic? 0
  • +

Replies To: How do I code password varifacation

#2 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8346
  • View blog
  • Posts: 31,904
  • Joined: 06-March 08

Re: How do I code password varifacation

Posted 13 April 2010 - 08:48 PM

This is how I would do it
and you while() is inside the do loop instead of terminating it

import java.util.Scanner; 

public class Password 
{ 
	// Create main method 
	public static void main(String[] args) 
	{ 
		// Declare variables 
		Scanner sc = new Scanner(System.in); 
		boolean goodPassword; 
		do 
		{ 
			// Prompt user to enter a possible password 
			System.out.println("Please enter a possible password."); 
			String password = sc.nextLine(); 
			goodPassword = validatePassword(password);
		} 	while(!goodPassword); 
	}

	private static boolean validatePassword(String password) {
		int length = password.length();
		if(length < 6)
			return false;
		if(length > 10)
			return false;
		int letters = 0, digits = 0;
		// 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; 
		} 
		if(letters == 0)
			return false;
		if(digits == 0)
			return false;
		return true;
	}
}


Was This Post Helpful? 0
  • +
  • -

#3 boosh  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 57
  • Joined: 14-January 10

Re: How do I code password varifacation

Posted 14 April 2010 - 12:18 AM

So when I run the code with the changes it still won't let me re-enter the password to validate it. So I go "Enter password"....blah123. It comes back with excepted password. Now re-enter password to verify. But it wont let me cause Im not loop savey.
Was This Post Helpful? 0
  • +
  • -

#4 boosh  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 57
  • Joined: 14-January 10

Re: How do I code password varifacation

Posted 18 April 2010 - 01:37 PM

I am still looking for help. Im sooo close i just cant get my app to verify the entered password. Any advice would be welcome.
Was This Post Helpful? 0
  • +
  • -

#5 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10803
  • View blog
  • Posts: 40,257
  • Joined: 27-December 08

Re: How do I code password varifacation

Posted 18 April 2010 - 01:58 PM

Can you post your updated code along with a specific description of your problem? Thanks for helping us help you! :)
Was This Post Helpful? 0
  • +
  • -

#6 boosh  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 57
  • Joined: 14-January 10

Re: How do I code password varifacation

Posted 19 April 2010 - 07:56 PM

View Postboosh, on 18 April 2010 - 12:37 PM, said:

I am still looking for help. Im sooo close i just cant get my app to verify the entered password. Any advice would be welcome.

Here is what I have so far. Im using Eclips to code in if that helps at all
import java.util.Scanner;

public class Password
{
	// 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 6 to 10 characters long, and contains 1 letter and 1 number.
			if (len < 6 || len > 10 || letters == 0 || digits == 0)
				System.out.println("Passwords must be 6 to 10 characters in lenght. Contain at least 1 letter, and 1 number. Please try again.");
			else
				System.out.println("Excepted. Please re-enter your password for verification.");
		}
		while (!goodPassword);
	}

	private Password()
	{
	}
}



<<<I am still not sure how to get this to return my password verification.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1