Skip to content

Instantly share code, notes, and snippets.

@luisoos
Last active January 12, 2022 17:27
Show Gist options
  • Save luisoos/b7f11236c0efe736299f8e48fbeb9764 to your computer and use it in GitHub Desktop.
Save luisoos/b7f11236c0efe736299f8e48fbeb9764 to your computer and use it in GitHub Desktop.
Java Password Generator
import java.util.Random;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
// Giving advice to user
System.out.println("How much characters should your password have?");
// Get user input
Scanner scanner = new Scanner(System.in);
int length = scanner.nextInt();
// Declaring available characters
String characters = "1234567890!§$%&/()=?QWERTZUIOPASDFGHJKLYXCVBNMqwertzuiopasdfghjklyxcvbnm.:,;-_#+^<>";
// Generate random password
Random random = new Random();
char[] password = new char[length];
for (int i = 0; i < length; i++) {
password[i] = characters.charAt(random.nextInt(characters.length()));
}
// Print out the password
System.out.println(password);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment