Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- public class pass {
- public static void main(String[] args) {
- //Define some variables
- int index = 0;
- int attempts = 0;
- Boolean access = false;
- Scanner scan = new Scanner(System.in);
- String names[] = {"Jonathan","Tracey","Mathew","Joey","Alex"};
- String passwords[] = {"password","123456","654321","joeyiscool","alex69"};
- System.out.println("Enter a name and password");
- String userName = scan.next();
- String uPass = scan.next();
- //Find the index of selected username
- for(int i = 0; i < names.length; i++){
- if(names[i].equals(userName)) {
- index = i;
- i = names.length;
- }
- }
- //Confine to while loop for number of attempts
- while(attempts < 1 && !access) {
- //Look for corresponding pass
- for(int i = 0; i < passwords.length; i++) {
- if(passwords[i].equals(uPass) && i == index)
- {
- System.out.println("Acess Granted");
- i = passwords.length;
- access = true;
- }
- }
- attempts++;
- if(!access)
- {
- System.out.println("Wrong name/password. Please try again");
- }
- uPass = scan.next();
- }
- if(!access)
- System.out.println("Sorry, your username or password does not match the database. Contact the administrator.");
- //At this point do something with access or exit program entirely
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement