When I try to type in a set user to add to the friends list, it shows up blank via [[]].
I know I'm missing something or it's the way I set up the input, but I can't figure it out.

Main Driver

import java.util.*;

public class main  
{

    public static void main(String[] args) 
    {
        Scanner input = new Scanner(System.in);

        int count = 0;

        FacebookUser user1 = new FacebookUser("anthony", "fenrir");

        System.out.println("Create your account");
        System.out.println("---------------------");
        System.out.println("Username: ");
        String userInput = input.next();
        System.out.println("Password: ");
        String passInput = input.next();
        System.out.println("Password Hint: ");
        String passHintInput = input.next();

        String username = userInput;
        String password = passInput;
        user1.setPasswordHint(passHintInput);

        System.out.println("\nLog in");
        System.out.println("---------------------");
        System.out.println("Username: ");
        String login = input.next();

        if (username.equals(login))
        {
            while (count < 2)
            {
                System.out.println("Password: ");
                login = input.next();

                if (password.equals(login))
                {
                    System.out.println(username.toString());
                    break;
                }
                else
                {
                    System.out.println("Incorrect password. Try again");
                    count ++;
                }

                if (count == 2)
                {
                user1.getPasswordHelp();
                System.out.println("Password: ");
                login = input.next();
                }
            } 

        }
        else
            System.out.println("Incorrect username.");

        ArrayList<FacebookUser> friendslist = new ArrayList<FacebookUser>();
        FacebookUser face = new FacebookUser("finn", "backpack");

        int choiceIn;

        do
        {
            choiceIn = displayMenu();
        if (choiceIn == 1)
        {
             System.out.println(user1.getFriends());

        }
        if (choiceIn == 2)
        {
            System.out.println("Enter in a friend to add.");
            System.out.println("Friend's username:");
            String name = input.next();

            user1.friend(face);
        }
        if (choiceIn == 3)
        {
            System.out.println("Enter in a friend to delete.");
            System.out.println("Friend's username:");
            String name = input.next();

            user1.defriend(face);
        }
        if (choiceIn == 4)
        {
            System.out.println("You have logged out.");
        }

        } while ((choiceIn >=1) && (choiceIn <= 3));


    }

    public static int displayMenu() 
    {
        Scanner input = new Scanner(System.in);
        int choice;

        System.out.println("\n Menu");
        System.out.println("1. List Friends");
        System.out.println("2. Add a User as a Friend");
        System.out.println("3. Delete a Friend");
        System.out.println("4. Quit");
        System.out.println("\n What would you like to do?");
        choice = input.nextInt();

        return choice;
    }


}

UserAccount Class

public abstract class UserAccount 
{

    private String username;
    private String password;
    private boolean active;


    public UserAccount(String username, String password) 
    {
        this.username = username;
        this.password = password;
        this.active = true;

    }

    public boolean checkPassword()
    {
        return password == this.password;
    }

    public void deactivateAccount()
    {
      active = false;
    }

    public String toString()
    {
        return  username;
    }

    @Override
    public boolean equals(Object obj)
    {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        UserAccount other = (UserAccount) obj;
        if (username == null) 
        {
            if (other.username != null)
                return false;
        } 
        else if (!username.equals(other.username))
            return false;
        return true;
    }

    public int hashCode(Object username)
    {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((username == null) ? 0: username.hashCode());
        return result;
    }

    public boolean isActive() 
    {
        return active;
    }

    public abstract void getPasswordHelp();


}

FacebookUser Class

import java.util.*;
public class FacebookUser extends UserAccount implements Comparable<FacebookUser>
{
    private String passwordHint;
    private ArrayList<FacebookUser> friends;

    public FacebookUser(String username, String password) 
    {
        super(username, password);
        friends = new ArrayList<>();
    }

    public void setPasswordHint(String passwordHint)
    {
        this.passwordHint = passwordHint;
    }


    public void friend(FacebookUser newFriend)
    {
        if (friends.contains(newFriend))
            System.out.println("Could not find this user to add.");
        else
            friends.add(newFriend);
    }

     public void defriend(FacebookUser formerFriend)
    {
        if (friends.contains(formerFriend))
            friends.remove(formerFriend);
        else
            System.out.println("There is no friend with this username");
    }

     ArrayList<FacebookUser> getFriends()
    {
         ArrayList<FacebookUser> friendsCopy = new ArrayList<FacebookUser>();

         for (int i = 0; i < friends.size(); i++) 
            {
                friendsCopy.add(friends.get(i));
            }

            Collections.sort(friendsCopy, new Comparator<FacebookUser>()
                    {
                        public int compare(FacebookUser v1, FacebookUser v2)
                        {
                            return v1.toString().compareTo(v2.toString());
                        }
                    });

            return friendsCopy;
    }
     //
    @Override
    public void getPasswordHelp() 
    {
        System.out.println("The password hint is: " + passwordHint);
    }


    @Override
    public int compareTo(FacebookUser o) 
    {
        return 0;
    }


    public String toString()
    {
        return friends.toString();
    }

}

Dani AI

Generated

Quick, concrete diagnosis for (and answering 's request for specifics): the printed output "[[]]" is caused by the FacebookUser.toString() override returning the friend's internal list. When System.out.println(user1.getFriends()) runs, Java prints the outer list and calls each friend's toString() — since each friend currently returns its own friends.toString() (empty), the result looks like a nested empty list. The simplest fix is to have FacebookUser print the username (or just use the superclass implementation):

@Override
public String toString() {
return super.toString(); // returns the username from UserAccount
}

A couple of other concrete issues to address in the main flow: the entered name is read but never used — the code always adds the single face object. Construct and add a new FacebookUser from the input (or change the friend API to accept a name). Example:

FacebookUser newFriend = new FacebookUser(name, "");
user1.friend(newFriend);

Also watch Scanner behavior (use nextLine() if names can contain spaces and handle the leftover newline after next()), and correct messages inside friend() (when contains is true the text should say "Friend already exists" rather than "Could not find this user"). Finally, consider these improvements for correctness and future work: implement public int hashCode() consistent with equals, change checkPassword to accept a candidate string and compare with equals, and have the sort comparator compare usernames explicitly instead of relying on toString().

Recommended Answers

All 2 Replies

can you be just a wee bit more specific ?

When the menu pops up I select add friend option so i can add finn to the friends list. And then I can choose the display option to display the friends I've added.

When I add finn, the name doesn't show up after I display it.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.