Skip to content

Instantly share code, notes, and snippets.

@raunaqsingh2020
Created January 10, 2020 17:28
Show Gist options
  • Save raunaqsingh2020/1f2fd8d0f05b681759dc07e4555c8c8c to your computer and use it in GitHub Desktop.
Save raunaqsingh2020/1f2fd8d0f05b681759dc07e4555c8c8c to your computer and use it in GitHub Desktop.
import java.util.*;
public class password
{
public password()
{
Scanner sc = new Scanner(System.in);
boolean playing = true;
System.out.println("Welcome to the password checker!");
while(playing){
String pass = "";
System.out.println("How long do you want your password to be?");
int count = sc.nextInt();
if(count < 4){
System.out.println("Must be at least 4 characters.");
count = sc.nextInt();
}
while(!checkPass(pass)){
pass = "";
for(int i = 0; i < count; i++){
pass = pass + randomChar();
}
}
System.out.println(pass);
System.out.println("Keep Going? ('Y' or 'N')");
String cont = sc.next();
if(cont.equals("N"))
playing = false;
}
}
public int randomNumber(int min, int max){
int rnd =(int)(Math.random() * ((max - min) )) + min;
return rnd;
}
public String randomChar()
{
String letters = "abcdefghijklmnopqrstuvwxyz";
String chars = "abcdefghijklmnopqrstuvwxyz0123456789!@#$%^&*?" + letters.toUpperCase();
return String.valueOf(chars.charAt(randomNumber(0,chars.length()-1)));
}
public boolean checkPass(String pass){
boolean lowercase = false;
boolean uppercase = false;
boolean hassymbol = false;
boolean hasnum = false;
String lower = "abcdefghijklmnopqrstuvwxyz";
String upper = lower.toUpperCase();
String nums = "0123456789";
String symbols = "!@#$%^&*?";
for(int i = 0; i < pass.length(); i++){
String testing = String.valueOf(pass.charAt(i));
if(lower.contains(testing))
lowercase = true;
if(upper.contains(testing))
uppercase = true;
if(nums.contains(testing))
hasnum = true;
if(symbols.contains(testing))
hassymbol = true;
}
return (lowercase && uppercase && hassymbol && hasnum);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment