Home > database >  Why are my accessor methods not returning any values from my arraylists?
Why are my accessor methods not returning any values from my arraylists?

Time:04-23

I am trying to code a simple account system in Java. I have declared two arraylists in the CreateAccount class that contain usernames and passwords. In the same class, I have written two getter methods that return the arraylists so they can be accessed from a different class. For some reason, they don't return any of the values in the arraylists when calling them from another class. I have even tried printing the arraylist inside of the getter methods but it prints no values, just a blank list. But when I try printing the lists inside of the addAccount method, it prints all the values as it should.

Here is the code for the CreateAccount class:

import java.util.*;

public class CreateAccount {
    private ArrayList<String> usernames = new ArrayList<String>();
    private ArrayList<String> passwords = new ArrayList<String>();
    
    
    public void addAccount(String username, String password) {
        
            if(usernames.contains(username.toLowerCase())) {
                System.out.println("Username already exists!");
            }   
            else if (username.contains(" ")) {
                System.out.println("Your username cannot contain spaces!");
            }
            else {
                usernames.add(username.toLowerCase());
                passwords.add(password.toLowerCase());
                System.out.println("Account successfuly created!");
                
            }
        System.out.println(usernames); //This prints out all values
    }
    
    public ArrayList<String> getUsernames(){
        System.out.println(usernames); //This prints out a blank list
        return usernames;
    }
    
    public ArrayList<String> getPasswords(){
        return passwords;
    }
    
    
}

Here is the code for the Login class:

import java.util.*;

public class Login {
    CreateAccount acc = new CreateAccount();
    private ArrayList<String> accountUsernames = acc.getUsernames();
    private ArrayList<String> accountPasswords = acc.getPasswords();
    
    public void testLogin(String username, String password) {

        System.out.println(accountUsernames); //This prints out a blank list
        System.out.println(accountPasswords); //This prints out a blank list
    }
        
}

CodePudding user response:

Ok, so I'm going to give some advice unrelated to the question. In most cases where you have usernames, you want them to be unique. A collection with a mapping of unique keys to a single value each is a Dictionary. In java, the two most common dictionaries are HashMap and Hashtable. Hashtables are synchronized, which means they are thread safe, ie. accessible from multiple threads.

A Hashtable<String, String> would make the intent of your code clearer, as well as performing much better, as Dictionaries have hash lookups of the keys (usernames).

The name of your class CreateAccount is an action, but what it does is handle accounts, not create them. AccountHandler would be a good name.

As stated by @fireshadow52, your code examples don't show adding any accounts. I'm not sure if that was a lack of full code in the question, or if it's legitimately the problem.

Here are some general improvements I'd implement.

import java.util.*;

public class AccountHandler {
    private Hashtable<String, String> accounts = new Hashtable<>();
    
    
    public void addAccount(String username, String password) {
        
            if(accounts.containsKey(username.toLowerCase())) {
                System.out.println("Username already exists!");
            }   
            else if (username.contains(" ")) {
                System.out.println("Your username cannot contain spaces!");
            }
            else {
                accounts.put(username.toLowerCase(), password.toLowerCase());
                System.out.println("Account successfuly created!");
                
            }
        System.out.println(accounts); //This prints out all values
    }
    
    public ArrayList<String> getUsernames(){
        System.out.println(usernames);
        return new ArrayList<String>(accounts.keys());
    }
    
    public ArrayList<String> getPasswords(){
        return new ArrayList<String>(accounts.values());
    }
    
    public Hashtable<String, String> getAccounts(){
        return accounts;
    }
}
import java.util.*;

public class Login {
    AccountHandler acc = new AccountHandler();
// Initializing variables at this scope will cause them to run before everything else 
    
    public void testLogin(String username, String password) {

        acc.addAccount("da3dsoul", "12345");
        System.out.println(acc.getUsernames());
        System.out.println(acc.getPasswords())
    }
        
}

I mentioned it briefly above, but by calling getUsernames and getPasswords at the top level scope, you cause them to run before anything is ever added to the account collection, as well.

CodePudding user response:

The code seems to be correct. I tried:

public static void main(String[] args){
    CreateAccount acc = new CreateAccount();
    acc.addAccount("John","John123");
    acc.addAccount("Oscar","Oscar123");

    ArrayList<String> accountUsernames = acc.getUsernames();
    ArrayList<String> accountPasswords = acc.getPasswords();

    System.out.println(accountUsernames);
    System.out.println(accountPasswords);
}

And I get the usernames and passwords as expected. You are currently not creating any accounts, therefore you get an empty list back. Try adding acc.addAccount("John","John123"); or something similar in the code and try getting the usernames and passwords after that. Hope this helps :)

  • Related