Skip to content

Instantly share code, notes, and snippets.

@feehe21
Last active November 30, 2018 22:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save feehe21/e76a67713f8283ec58ae4c8a352248b6 to your computer and use it in GitHub Desktop.
Save feehe21/e76a67713f8283ec58ae4c8a352248b6 to your computer and use it in GitHub Desktop.
/**
* various math equations
*
* @author (Erica)
* @version (9/18/18)
*/
public class objectClass
{
public objectClass(){
}
public int getRandom(int min, int max){
return (int)(Math.random() * ((max-min) + 1)) + min;
}
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
import java.util.*;
public class runnerClass
{
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
objectClass oc = new objectClass();
//int rand = oc.getRandom(1,10);
//System.out.println(rand);
String letters = "abcdefghijklmnopqrstuvwxyz";
String numbers = "0123456789";
String spec = "!@#$%^&*?";
int count = 0;
int pick;
int rand;
String password = "Password: ";
int lettersUsed = 0;
int numbersUsed = 0;
int specUsed = 0;
int upperUsed = 0;
System.out.println("How long do you want the minimum length of your password to be?");
System.out.println("Make it at least 8 long.");
int length = -1;
while(length == -1){
length = bigEnough(scan.nextInt());
}
while (count < length){
pick = oc.getRandom(0,3);
if(pick == 0){
lettersUsed++;
rand = oc.getRandom(1, letters.length()-1);
password = password + letters.substring(rand-1, rand);
}else if(pick == 1){
numbersUsed ++;
rand = oc.getRandom(1, numbers.length()-1);
password = password + numbers.substring(rand-1, rand);
}else if(pick == 2){
specUsed ++;
rand = oc.getRandom(1, spec.length()-1);
password = password + spec.substring(rand-1, rand);
}else{
upperUsed ++;
rand = oc.getRandom(1, letters.length()-1);
password = password + (letters.substring(rand-1, rand)).toUpperCase();
}
count++;
}
if(lettersUsed == 0 ){
rand = oc.getRandom(1, letters.length());
password = password + letters.substring(rand-1, rand);
}
if(numbersUsed == 0 ){
rand = oc.getRandom(1, numbers.length());
password = password + numbers.substring(rand-1, rand);
}
if(specUsed == 0 ){
rand = oc.getRandom(1, spec.length());
password = password + spec.substring(rand-1, rand);
}
if(upperUsed == 0 ){
rand = oc.getRandom(1, letters.length()-1);
password = password + (letters.substring(rand-1, rand)).toUpperCase();
}
System.out.println(password);
}
public static int bigEnough(int x){
if(x >= 8){
return x;
}else{
System.out.println("That's not long enough");
return -1;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment