import java.lang.*;
import java.io.*;
import java.util.Scanner;
public class PasswordVerifier
{
public static void main(String[] args)
{
System.out.println("Password Verifier \n"); //print name of program
System.out.println("Enter a password that meets the following rules: "); //print follow rules
System.out.println("Must be at least 7 characters long "); //print rule
System.out.println("Must contain at least 1 lower letter character "); //print rule
System.out.println("Must contains at least 1 upper letter character "); //print rule
System.out.println("Must contains at least 1 numeric digit "); //print rule
System.out.println("Must contains at least 1 special character from the set: !@#$%^&*: "); //print rule
System.out.println("Does not contain the word (pass) or the word (word): "); //print rule
String password;
String contains1= "pass";
String contains2= "word";
Scanner scnr = new Scanner(System.in);
boolean digit = false; //number variable
boolean upper = false; //upper case variable
boolean lower = false; //lower case variable
boolean foundIt = false; //make sure all neccessary variables are in password
System.out.println("Enter password: ");
password = scnr.next();
char[] array = password.toCharArray();
char[] specialCh = {'!','@',']','#','$','%','^','&','*'};
if (password.length() > 7) //required password length
{
int result;
result = password.indexOf (contains1);
if (result== -1)
{
System.out.println ("");
}
else
{
System.out.println("Must not contain the word pass");
}
int result2;
result2 = password.indexOf (contains2);
if (result2== -1)
{
System.out.println ("");
}
else
{
System.out.println("Must not contain the word word");
}
for
(int i=0; i<array.length; i++) //checking for length
{
if (Character.isDigit(array[i]))//checking for numbers
{
digit = true; //if it has it keep going
}
else if(Character.isLowerCase (array[i])) //checking for lower case
{
lower = true; //if it has it keep going
}
else if(Character.isUpperCase (array[i]))//checking for upper case
{
upper = true;//if it has it keep going
}
else if (specialCh
}
}
else
{
System.out.println("Please enter a password that is longer than 7 characters");//invalid if it doesn't contain 7 letters
}
if (digit && lower && upper) //valid if it contains the proper upper lower and number required
{
System.out.println("Your password is valid.");
}
else
{
System.out.println("Your password is invalid. Please try again");
}
}
}
9 Replies - 9180 Views - Last Post: 19 September 2011 - 05:59 AM
#1
How do I ask if there is a special character !@#$%^&*
Posted 18 September 2011 - 11:46 AM
Replies To: How do I ask if there is a special character !@#$%^&*
#2
Re: How do I ask if there is a special character !@#$%^&*
Posted 18 September 2011 - 12:00 PM
you can make a nested for-each loop that runs through all of your special characters, or you can use a regex (a little more complicated to understand).
boolean hasSpecialChar = false;
char current = array[i];
for (Character c : specialCh) {
if (current == c)
hasSpecialChar = true;
}
#3
Re: How do I ask if there is a special character !@#$%^&*
Posted 18 September 2011 - 12:29 PM
thanks CasiOo would you elaborating a little bit i mean how do I add the characters I want to check
#4
Re: How do I ask if there is a special character !@#$%^&*
Posted 18 September 2011 - 12:34 PM
You simply write them in your char array like you have here:
The loop goes through every character in the specialCh array.
It is like writing
char[] specialCh = {'!','@',']','#','$','%','^','&','*'};
The loop goes through every character in the specialCh array.
It is like writing
for (int i=0; i<specialCh.length; i++) {
char c = specialCh[i];
}
This post has been edited by CasiOo: 18 September 2011 - 12:35 PM
#5
Re: How do I ask if there is a special character !@#$%^&*
Posted 18 September 2011 - 12:53 PM
having trouble inserting it could i get some guidance
import java.lang.*;
import java.io.*;
import java.util.Scanner;
public class PasswordVerifier
{
public static void main(String[] args)
{
System.out.println("Password Verifier \n"); //print name of program
System.out.println("Enter a password that meets the following rules: "); //print follow rules
System.out.println("Must be at least 7 characters long "); //print rule
System.out.println("Must contain at least 1 lower letter character "); //print rule
System.out.println("Must contains at least 1 upper letter character "); //print rule
System.out.println("Must contains at least 1 numeric digit "); //print rule
System.out.println("Must contains at least 1 special character from the set: !@#$%^&*: "); //print rule
System.out.println("Does not contain the word (pass) or the word (word): "); //print rule
String password;
String contains1= "pass";
String contains2= "word";
Scanner scnr = new Scanner(System.in);
boolean digit = false; //number variable
boolean upper = false; //upper case variable
boolean lower = false; //lower case variable
boolean foundIt = false; //make sure all neccessary variables are in password
boolean SpecialCh = false;
System.out.println("Enter password: ");
password = scnr.next();
char[] array = password.toCharArray();
char current = array[i];
char[] specialCh = {'!','@',']','#','$','%','^','&','*'};
if (password.length() > 7) //required password length
{
int result;
result = password.indexOf (contains1);
if (result== -1)
{
System.out.println ("");
}
else
{
System.out.println("Must not contain the word pass");
}
int result2;
result2 = password.indexOf (contains2);
if (result2== -1)
{
System.out.println ("");
}
else
{
System.out.println("Must not contain the word word");
}
for
(int i=0; i<array.length; i++) //checking for length
{
if (Character.isDigit(array[i]))//checking for numbers
{
digit = true; //if it has it keep going
}
else if(Character.isLowerCase (array[i])) //checking for lower case
{
lower = true; //if it has it keep going
}
else if(Character.isUpperCase (array[i]))//checking for upper case
{
upper = true;//if it has it keep going
}
(Character c : specialCh)
{
if (current == c)
SpecialCh = true;
}
}
}
else
{
System.out.println("Please enter a password that is longer than 7 characters");//invalid if it doesn't contain 7 letters
}
if (digit && lower && upper) //valid if it contains the proper upper lower and number required
{
System.out.println("Your password is valid.");
}
else
{
System.out.println("Your password is invalid. Please try again");
}
}
}
#6
Re: How do I ask if there is a special character !@#$%^&*
Posted 18 September 2011 - 07:00 PM
Can someone help me adjust this code so that when the array goes through it checks for the special characters someone has helped me out on the code for the special characters but i don't know how implement it in the loop.
import java.lang.*;
import java.io.*;
import java.util.Scanner;
public class PasswordVerifier
{
public static void main(String[] args)
{
System.out.println("Password Verifier \n"); //print name of program
System.out.println("Enter a password that meets the following rules: "); //print follow rules
System.out.println("Must be at least 7 characters long "); //print rule
System.out.println("Must contain at least 1 lower letter character "); //print rule
System.out.println("Must contains at least 1 upper letter character "); //print rule
System.out.println("Must contains at least 1 numeric digit "); //print rule
System.out.println("Must contains at least 1 special character from the set: !@#$%^&*: "); //print rule
System.out.println("Does not contain the word (pass) or the word (word): "); //print rule
String password;
String contains1= "pass";
String contains2= "word";
Scanner scnr = new Scanner(System.in);
boolean digit = false; //number variable
boolean upper = false; //upper case variable
boolean lower = false; //lower case variable
boolean foundIt = false; //make sure all neccessary variables are in password
boolean SpecialCh = false;
System.out.println("Enter password: ");
password = scnr.next();
char[] array = password.toCharArray();
char current = array[c];
char[] specialCh = {'!','@',']','#','$','%','^','&','*'};
if (password.length() > 7) //required password length
{
int result;
result = password.indexOf (contains1);
if (result== -1)
{
System.out.println ("");
}
else
{
System.out.println("Must not contain the word pass");
}
int result2;
result2 = password.indexOf (contains2);
if (result2== -1)
{
System.out.println ("");
}
else
{
System.out.println("Must not contain the word word");
}
for(int i=0; i<specialCh.length; i++)
{
char c = specialCh[i];
}
//(Character c : specialCh) {
if (current == c)
SpecialCh = true;
for
(int i=0; i<array.length; i++) //checking for length
{
if (Character.isDigit(array[i]))//checking for numbers
{
digit = true; //if it has it keep going
}
else if(Character.isLowerCase (array[i])) //checking for lower case
{
lower = true; //if it has it keep going
}
else if(Character.isUpperCase (array[i]))//checking for upper case
{
upper = true;//if it has it keep going
}
}
}
else
{
System.out.println("Please enter a password that is longer than 7 characters");//invalid if it doesn't contain 7 letters
}
if (digit && lower && upper) //valid if it contains the proper upper lower and number required
{
System.out.println("Your password is valid.");
}
else
{
System.out.println("Your password is invalid. Please try again");
}
}
}
#7
Re: How do I ask if there is a special character !@#$%^&*
Posted 18 September 2011 - 07:10 PM
You could loop through your array and do this:
The method .contains check if a specified set of characters are contained in a String. All in the documentation bro.
http://download.oracle.com/javase/1,5.0/docs/api/java/lang/String.html
EDIT:
My bad, .contains already returns a boolean, so just do what you want when the string contains special characters, ignore the "prudent" thing I told ya.
for(int x = 0; x < arrayOfSpecialChars.length ; x++){
if(string.contains(arrayOfSpecialChars[x]){
//do something here, like maybe make the PC explode or something
//But the prudent way to do it is make a boolean and return true that a string contains special chars
//Or, you could print the message "Hey you, no SP3C!@L CH@RZ lolz" or your message in here
}
}
The method .contains check if a specified set of characters are contained in a String. All in the documentation bro.
http://download.oracle.com/javase/1,5.0/docs/api/java/lang/String.html
EDIT:
My bad, .contains already returns a boolean, so just do what you want when the string contains special characters, ignore the "prudent" thing I told ya.
This post has been edited by fromTheSprawl: 18 September 2011 - 07:12 PM
#8
Re: How do I ask if there is a special character !@#$%^&*
Posted 18 September 2011 - 11:55 PM
I've read your duplicate thread, wow "Super" huh. Anyway I'll help you so hopefully in the future you won't make the same mistakes again.
We need to achieve two things in your code right?
1. Have at least 1 special character on the password
2. Check if "pass" and "word" exists in the password string
To help you solve this problem you'll need to utilize these methods from the String object:
.contains() - check if a string contains a certain string
.equals() - check if the current String object has the same value as the object being compared to it
.toLower() or .toUpper - converts the string into lowercase and uppercase respectively
First! Check if there is a special character on the password.
Now see this code:
I don't know what the difference is between next and nextLine, but I use nextLine all the time.
Now your password variable contains the input of the user. Let us do the first checking!
Let's see if there's a special character in the string.
Now we can use the boolean variable to manipulate the user.
Now let's check if the password is "pass" or "word"!
Now why do we need to do this? To simplify checking of course. Other than checking if a password is equals to "PASS" or "pass" or "pAsS" you get the point, we could do this:
See, that was cleaner. Now if you mean "pass" or "word" shouldn't be contained anywhere on the password I don't see the logic in that. But if you still insist, check what we did with the special characters. I hope you figure it out.
We need to achieve two things in your code right?
1. Have at least 1 special character on the password
2. Check if "pass" and "word" exists in the password string
To help you solve this problem you'll need to utilize these methods from the String object:
.contains() - check if a string contains a certain string
.equals() - check if the current String object has the same value as the object being compared to it
.toLower() or .toUpper - converts the string into lowercase and uppercase respectively
First! Check if there is a special character on the password.
Now see this code:
Scanner sc = new Scanner(System.in);
String password = new String();
System.out.println("Please enter a valid password!");
password = sc.nextLine();
I don't know what the difference is between next and nextLine, but I use nextLine all the time.
Now your password variable contains the input of the user. Let us do the first checking!
Let's see if there's a special character in the string.
boolean specialCharIsFound = false;
for(int x = 0; x <= specialChars.length; x++)
{
if(password.contains(specialChars[x])){
specialCharIsFound = false;
break;
}
}
Now we can use the boolean variable to manipulate the user.
if(specialCharIsFound == true){
//do nothing or anything you want when a password is valid, like check the next validation
}
else{
System.out.println("Password doesn't contain a special character! Please add at least 1");
//Then make the PC restart or something.
// Actually you could wrap all your input codes inside a do - while loop, but for now let's stick to the basics and help you validate that password ^^/>
}
Now let's check if the password is "pass" or "word"!
String passwordCopy = password.toLower();// Which will give our copy a lowercase version of password.
Now why do we need to do this? To simplify checking of course. Other than checking if a password is equals to "PASS" or "pass" or "pAsS" you get the point, we could do this:
if(passwordCopy.equals("pass")==true||passwordCopy.equals("word")==true){
//Then the password is invalid!
}
See, that was cleaner. Now if you mean "pass" or "word" shouldn't be contained anywhere on the password I don't see the logic in that. But if you still insist, check what we did with the special characters. I hope you figure it out.
#9
Re: How do I ask if there is a special character !@#$%^&*
Posted 19 September 2011 - 04:21 AM
ch0senw0n, on 18 September 2011 - 08:53 PM, said:
having trouble inserting it could i get some guidance
(Character c : specialCh)
{
if (current == c)
SpecialCh = true;
}
This is a for loop, so should be
for (Character c : specialCh)
{
if (current == c)
SpecialCh = true;
}
You also don't check to see if SpecialCh is false to verify if the password is valid. Also, I would change SpecialCh to something else (hasSpecialChar, for example). Variables in java should always start lowercase, and you already have a specialCh variable. Accidently using the wrong one could lead to bugs/errors that are hard to find later on.
#10
Re: How do I ask if there is a special character !@#$%^&*
Posted 19 September 2011 - 05:59 AM
Duplicate threads merged. Please do NOT open duplicate threads.
Page 1 of 1
|
|

New Topic/Question
Reply



MultiQuote








|