Advertisement
Guest User

Untitled

a guest
Oct 19th, 2017
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.30 KB | None | 0 0
  1. import java.util.Scanner;
  2. public class pass {
  3.  
  4.     public static void main(String[] args) {
  5.         //Define some variables
  6.         int index = 0;
  7.         int attempts = 0;
  8.         Boolean access = false;
  9.         Scanner scan = new Scanner(System.in);
  10.         String names[] = {"Jonathan","Tracey","Mathew","Joey","Alex"};
  11.         String passwords[] = {"password","123456","654321","joeyiscool","alex69"};
  12.         System.out.println("Enter a name and password");
  13.         String userName = scan.next();
  14.         String uPass = scan.next();
  15.         //Find the index of selected username
  16.         for(int i = 0; i < names.length; i++){
  17.             if(names[i].equals(userName)) {
  18.                 index = i;
  19.                 i = names.length;
  20.             }
  21.         }
  22.         //Confine to while loop for number of attempts
  23.         while(attempts < 1 && !access) {
  24.             //Look for corresponding pass
  25.         for(int i = 0; i < passwords.length; i++) {
  26.             if(passwords[i].equals(uPass) && i == index)
  27.             {
  28.                 System.out.println("Acess Granted");
  29.                 i = passwords.length;
  30.                 access = true;
  31.             }  
  32.         }
  33.             attempts++;
  34.             if(!access)
  35.             {
  36.                 System.out.println("Wrong name/password. Please try again");
  37.             }
  38.             uPass = scan.next();
  39.         }
  40.         if(!access)
  41.         System.out.println("Sorry, your username or password does not match the database. Contact the administrator.");
  42.         //At this point do something with access or exit program entirely
  43.  
  44.     }
  45. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement