Question

In: Computer Science

create a program that will verify a user's login credentials. The program should prompt the user...

  1. create a program that will verify a user's login credentials. The program should prompt the user to enter his/her username and password at the keyboard. Then it should read the data from a data file named "login.txt". The file "login.txt" will contain a list of 3 valid usernames and passwords to verify the login information supplied by a user.  If the username and password entered by the user matches one of the sets read from the file, the program should print a message "You have successfully logged in...welcome!!" If not, then the program should print a message "Sorry, incorrect login information. Access denied".

The file "login.txt" should contain the following lines of text:

jramerez WhatsUp22!!

kanderson HeyDude23!

mjones ChillOut24!!

Your program should produce output similar to the following (here are two sample runs, one where login failed and one where it succeeded):

__________________________________________________________________________________________________________

Please enter your username and password seperated by a space:

asaprocky Hello!234

Sorry, incorrect login information. Access denied.

_____________________________________________________________________________________________________________________________

Please enter your username and password:

kanderson HeyDude23!

"You have successfully logged in... Welcome!!

_____________________________________________________________________________________________________________________________

**Hints:  

  • Create the  file "login.txt" with a text editor such that it contains the 3 lines of data given above. Save the file to your project folder in your eclipse workspace.
  • Use 2 Scanner objects, one to read from the keyboard, and one to read from the file "login.txt"

Solutions

Expert Solution

I have run this code in NetBeans IDE.

package login;

import java.util.Scanner;
import java.io.*;

public class Login {

    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);
        System.out.println("Please enter your username and password seperated by a space");
        String name = sc.nextLine();
        //split the input entered by user with space
        String[] tokens = name.split(" ");
        
        //first element is username
        String username = tokens[0];
        
        //second element is password
        String password = tokens[1];
        
        //declare booleand variable valid to false
        boolean valid = false;
        try {
            
            //login.txt is saveed in d directory in my pc
            //below line you have to provide the path of login.txt stored in your pc
            File myObj = new File("D:\\login.txt");
            Scanner myReader = new Scanner(myObj);

            //reads file until lastline
            while (myReader.hasNextLine()) {

                String data = myReader.nextLine();
                
                //split the username and password seperated by space
                String[] filedata = data.split(" ");
                String file_uname = filedata[0];
                String file_pwd = filedata[1];

                //checks is username and password entered by user is present in file
                //if it is present in file then set valid to true and break the loop
                if (username.equals(file_uname) && password.equals(file_pwd)) {
                    valid = true;              
                    break;
                } 
            }
            myReader.close();

        } catch (FileNotFoundException e) {
            System.out.println("An error occurred.");
            e.printStackTrace();
        }
        
        //if valid is set to true then credentials are valid
        if(valid)
        {
             System.out.println("You have successfully logged in... Welcome!!");
        }
        else{
            System.err.println("Sorry, incorrect login information. Access denied");
        }
    }
}

OUTPUT:


Related Solutions

C++ : Write a program that creates a login name for a user, given the user's...
C++ : Write a program that creates a login name for a user, given the user's first name, last name, and a four-digit integer as input. Output the login name, which is made up of the first five letters of the last name, followed by the first letter of the first name, and then the last two digits of the number (use the % operator). If the last name has less than five letters, then use all letters of the...
Create a program using python that provides a simple calculator: Requires a login with a prompt...
Create a program using python that provides a simple calculator: Requires a login with a prompt for a username and a password prior to using the calculator If username and password are incorrect, the program should re-prompt to re-enter correct username and password Once logged in, the user should have access to the calculator and should have the ability for the following mathematical operators: Addition Subtraction Multiplication Division Square Root PI Exponents
Create in Java a program that will prompt the user to enter aweight for a...
Create in Java a program that will prompt the user to enter a weight for a patient in kilograms and that calculates both bolus and infusion rates based on weight of patient in an interactive GUI application, label it AMI Calculator. The patients weight will be the only entry from the user. Use 3999 as a standard for calculating BOLUS: To calculate the BOLUS you will multiply 60 times the weight of the patient for a total number. IF the...
Create in java a program that will prompt the user to enter a weight for a...
Create in java a program that will prompt the user to enter a weight for a patient in kilograms and that calculates infusion rates based on weight of patient in an interactive GUI application, label it HEPCALC. The patients’ weight will be the only entry from the user. To calculate the infusion rate you will multiply 12 times the weight divided by 50 for a total number. The end result will need to round up or down the whole number....
Write a program that calculates the salary of employees. The program should prompt the user to...
Write a program that calculates the salary of employees. The program should prompt the user to enter hourly rate and number of hours of work a day. Then, the program should display the salary daily, bi-weekly (5 days a week), and monthly. Sample program: Enter your hourly rate: >>> 20 Enter how many hours you work a day: >>> 8 Your daily salary is: $160 Your bi-weekly salary is: $1600 Your monthly: $3200
Program should be written in Java b) The computer program should prompt the user (You are...
Program should be written in Java b) The computer program should prompt the user (You are the user) to enter the answers to the following questions: What is your city of birth? What is your favorite sport? If you could live anywhere in the world, where would you like to live? What is your dream vacation? Take this information and create a short paragraph about the user and output this paragraph. You may use the Scanner class and the System.out...
5) Create the following in a Java program Create a scanner Prompt the user to enter...
5) Create the following in a Java program Create a scanner Prompt the user to enter the name where the box of mail is shipping from and create the variable and relate to scanner Prompt the user to enter the name of destination where the box of mail will be shipped and create the variable and relate to scanner Prompt the user to enter the weight of the package and create variable to relate to scanner Calculate cost of shipping...
Create a C++ program that will prompt the user to input an integer number and output...
Create a C++ program that will prompt the user to input an integer number and output the corresponding number to its numerical words. (From 0-1000000 only) **Please only use #include <iostream>, switch and if-else statements only and do not use string storing for the conversion in words. Thank you.** **Our class is still discussing on the basics of programming. Please focus only on the basics. Thank you.** Example outputs: Enter a number: 68954 Sixty Eight Thousand Nine Hundred Fifty Four...
Create a program that will prompt for user information for a Web site. Use a structure...
Create a program that will prompt for user information for a Web site. Use a structure to store the data. The structure will need to include the following items: First Name - string Last Name - string Full Name - string Birth Date   - int (assume format yyyymmdd) IsLeasingAutomobile - bool yearlySalary - float Create a single structure from the items listed above. Prompt for all items except "Full Name" and load the input directly into a variable based on...
6) Create the following in a Java Program: Create payroll system Prompt the user to enter...
6) Create the following in a Java Program: Create payroll system Prompt the user to enter payroll information Employee name and create variable for scanner Hours worked Hourly pay rate Federal tax rate State tax rate Declare variable doubles for gross pay, federal, state and total deduction Display payroll statement example: Name of employee Hours worked Hourly pay rate Gross pay which is equal hours worked multiply hourly pay rate Federal withholding which is equal of gross pay multiply federal...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT