In: Computer Science
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:
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:

