Search code examples
javaeclipseauthenticationjava.util.scannerjava-17

How to make a library of string arrays and access them like a login system. (Java 17)


I am trying to make a login prompt with usernames and passwords, but I cannot figure out how to make the string array library. My current code can take in 1 username and password by using functions with variables, (like public myFunction(String myVariable);) and login with just that, and change the pass and username. currently, my code is

package main;

import java.util.Scanner;
import logins.Logins;

public class Main {
    public static void main(String[] args) {
        String choice = null;
        Scanner scanner = null;
        start(choice, scanner, "Password", "Username");
    }
    
    public static void command(String choice, Scanner scanner, String pass, String user) {
        if(choice.equals("login")) { 
            login(pass, scanner, choice, user);
        }else if(choice.equals("change password")) {
            passChange(pass, choice, scanner, user);
        }else if(choice.equals("change username")) {
            userChange(pass, choice, scanner, user);
        }else {
            System.err.println("Invalid choice");
            start(choice, scanner, pass, user);
        }
    }
    
    public static void password(String pass, String choice, Scanner scanner, String user) {
        System.out.println("Enter password");
        choice = scanner.nextLine();
        if(choice.equals(pass)) {
            System.out.println("Password Correct. You are logged in");
            start(choice, scanner, pass, user);
        }else {
            System.err.println("Incorrect Password");
            start(choice, scanner, pass, user);
        }
    }
    
    public static void passChange(String pass, String choice, Scanner scanner, String user) {
        System.out.println("Username:");
        choice = scanner.nextLine();
        if (choice.equals(user)) {
            System.out.println("Old Password:");
            choice = scanner.nextLine();
            if (choice.equals(pass)) {
                System.out.println("New Password");
                choice = scanner.nextLine();
                pass = choice;
                start(choice, scanner, pass, user);
            }else {
                System.err.println("Incorrect password");
                start(choice, scanner, pass, user);
            }
        }else {
            System.err.println("No user found named " + choice);
            start(choice, scanner, pass, user);
        }
    }
    
    public static void userChange(String pass, String choice, Scanner scanner, String user) {
        System.out.println("Username:");
        choice = scanner.nextLine();
        if (choice.equals(user)) {
            System.out.println("Password:");
            choice = scanner.nextLine();
            if (choice.equals(pass)) {
                System.out.println("Logged in. New username:");
                choice = scanner.nextLine();
                user = choice;
                start(choice, scanner, pass, user);
            }else {
                System.err.println("Incorrect password");
                start(choice, scanner, pass, user);
            }
        }else {
            System.err.println("Incorrect password");
            start(choice, scanner, pass, user);
        }
    }
    
    public static void start(String choice, Scanner scanner, String pass, String user) {
        scanner = new Scanner(System.in);
        System.out.println("What would you like to do? ");
        choice = scanner.nextLine();
        command(choice, scanner, pass, user);
    }
    public static void login(String pass, Scanner scanner, String choice, String user) {
        System.out.println("Enter username");
        choice = scanner.nextLine();
        if(choice.equals(user)) {
            System.out.println("Valid Username.");
            password(pass, choice, scanner, user);
        }else {
            System.err.println("No user named " + choice);
            start(choice, scanner, pass, user);
        }
    }
}

I have started making an array library, but I don't know how to access any of the usernames or passwords in general,(like once you put in a username, it gets the matching password)

package logins;

public class Logins {
    public static void main(String[] args) {
        String[] User = new String[] {"Username", "Password"};
        String[] User1 = new String[] {"Username1", "Password1"};
    }
}


Solution

  • Are you trying to link Usernames and Passwords together? If so, try using a HashMap. You could try doing something like:

    HashMap<String, String> logins= new HashMap<String, String>();
    

    This way a username can retrieve the corresponding password

    String password = logins.get("Mario");
    

    Though getting passwords from usernames would not be recommended. You could add to it by:

    logins.put("Mario", "password1234");
    

    Not sure if this is what you were asking but hope this helps