Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.files;
- import java.io.File;
- import java.io.FileNotFoundException;
- import java.io.FileWriter;
- import java.io.IOException;
- import java.io.PrintWriter;
- import java.util.ArrayList;
- import java.util.Scanner;
- public class Login_Register {
- Scanner input = new Scanner(System.in);
- ArrayList<String> datafile = new ArrayList<String>();
- public void dataRetrieve() {
- try {
- Scanner retrieve = new Scanner(new File(
- "C:\\Users\\Fujitsu\\Documents\\Java Files\\Data Structure and Algo\\src\\com\\files\\records.txt"));
- while (retrieve.hasNext()) {
- datafile.add(retrieve.next());
- }
- retrieve.close();
- } catch (FileNotFoundException e) {
- e.printStackTrace();
- }
- }
- // This is the method for register
- public void resgister() {
- dataRetrieve();
- try {
- // Getting input from the user
- System.out.print("Enter your username: ");
- String username = input.nextLine();
- System.out.print("Enter your password: ");
- String password = input.nextLine();
- // String data = username + " " + password;
- datafile.add(username);
- datafile.add(password);
- input.close();
- PrintWriter writer = new PrintWriter(new FileWriter(
- "C:\\Users\\Fujitsu\\Documents\\Java Files\\Data Structure and Algo\\src\\com\\files\\records.txt"));
- for (String passingData : datafile) {
- writer.println(passingData);
- }
- writer.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- // This is the method for register
- public void login() {
- Boolean found = false;
- String tempUsername = "";
- String tempPassword = "";
- int attempt = 5;
- try {
- Scanner reader = new Scanner(new File(
- "C:\\Users\\Fujitsu\\Documents\\Java Files\\Data Structure and Algo\\src\\com\\files\\records.txt"));
- System.out.println("Login\n");
- while (reader.hasNext() && !found) {
- System.out.print("Username: ");
- String username = input.nextLine();
- System.out.print("Password: ");
- String password = input.nextLine();
- tempUsername = reader.next();
- tempPassword = reader.next();
- if (tempUsername.trim().equals(username.trim()) && tempPassword.trim().equals(password.trim())) {
- System.out.println("\n--------------------------------");
- System.out.println("\nSuccessfully logged in");
- System.out.println("Hello " + username + " Welcome.");
- found = true;
- break;
- }else {
- System.out.println("Incorrect username or password");
- }
- }
- input.close();
- reader.close();
- } catch (FileNotFoundException e) {
- e.printStackTrace();
- }
- }
- public static void main(String[] args) {
- Scanner input = new Scanner(System.in);
- Login_Register lr = new Login_Register();
- System.out.println("[1] - Register\n[2] - Login\nEnter your choice: ");
- int choice = input.nextInt();
- switch (choice) {
- case 1:
- lr.resgister();
- break;
- case 2:
- lr.login();
- break;
- default:
- System.out.println("Invalid input!");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement