Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Create a text file that stores a list of user names and their associated passwor

ID: 3673270 • Letter: C

Question

Create a text file that stores a list of user names and their associated passwords. Write a Java program that will ask a user to enter their username and password and compares against the ones stored in the file. If their is a match, print "Access Granted" or else print "Access Denied". Make sure that the password is case sensitive. In case there isn't a match,the user gets two more attempts. If the user input doesn't match in all the three attempts, your program should display "Three failed attempts, your account has been locked"

Explanation / Answer

/**
* The java program PasswordMatching that prompts user to enter
* the name and password . If user name and password are matched
* print "Access Granted " otherwise print "Access Denied".
* Allow user to enter two more trials . If failed to enter
* correct password , the print that account has been locked.
* **/
//PasswordMatching.java
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class PasswordMatching
{
   public static void main(String[] args)
   {
      
      
       //set number of lines in the text file
       final int LINES=4;
       //text file name
       String fileName="passwords.txt";
       //create keyboard object
       Scanner keyboard=new Scanner(System.in);
       Scanner filereader=null;
       int row=0;
       //Create a 2D array of type String
       String[][] passwords=new String[LINES][2];


       //Open file in try catch block
       try
       {
           //Create an instance of Scanner class to read file
           filereader=new Scanner(new File(fileName));
           //read file into array passwords
           while(filereader.hasNextLine())
           {
               //read name from text file
               String name=filereader.next();
               //read password from text file
               String password=filereader.next();
              
               //set name and passwords in a passwords 2D array
               //of 4 rows and 2 columns
               passwords[row][0]=name;
               passwords[row][1]=password;

               //increment row by one
               row++;
           }

           //close the filereader object
           filereader.close();
       }
       //Catch the exception if file not found
       catch (FileNotFoundException e)
       {
           System.out.println("File not found.");
       }

       int CHOICES=3;

       int trials=1;
       boolean found=false;
      
       //Run while loop until trials are less than or equal to three
       //and found is false
       while(trials<=CHOICES &&!found)
       {
          
           System.out.println("Enter user name ");
           //read userName
           String userName=keyboard.nextLine();

           System.out.println("Enter password ");
           //user password
           String password=keyboard.nextLine();

           found=false;
           for (int i = 0; i < passwords.length && !found; i++)
           {
               if(passwords[i][0].equals(userName)
                       && passwords[i][1].equals(password))
               {
                   //set found true
                   found=true;

               }
           }
           //Check if found is true
           if(found)
               System.out.println("Access Granted");
           else
               System.out.println("Access Denied");  
          
           //increment the number of trials by one
           trials++;
       }
      
       //print message if found is false and trials are three
       if(!found && trials==3)
           System.out.println("Three failed attempts, your account has been locked");

   }
}


------------------------------------------------------------------------------------------------

Sample Input file

passwords.txt

johnson 1234
karan 4567
Kete 3242
Micheal 9009

--------------------------------------------------------------------------------------------------------------------

Sample Output:

Enter user name
johson
Enter password
1235
Access Denied
Enter user name
johson
Enter password
3456
Access Denied
Enter user name
johnson
Enter password
1234
Access Granted