Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 572291
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T13:35:52+00:00 2026-05-13T13:35:52+00:00

I have the following two classes: import java.io.*; import java.util.*; public class User {

  • 0

I have the following two classes:

import java.io.*;
import java.util.*;

public class User {

    public static String nickname;
    public static String ipAddress;
    public static ArrayList<String> listOfFiles;
    public static File sharedFolder;
    public static String fileLocation;

    public User(String nickname, String ipAddress, String fileLocation) {

        this.nickname = nickname.toLowerCase();
        this.ipAddress = ipAddress;

        Scanner userTyping = new Scanner(System.in);
        fileLocation = userTyping.nextLine();

        sharedFolder = new File(fileLocation);

    }

    public static List<String> fileList() {

        File[] files = sharedFolder.listFiles();

        listOfFiles = new ArrayList<String>();

        for (int i = 0; i < files.length; i++) {

            listOfFiles.add(i, files[i].toString().substring(fileLocation.length()));
            System.out.println(listOfFiles.get(i));

        }

       return listOfFiles;

    }

    @Override
    public String toString() {
        return nickname + " " + ipAddress;
    }



}

and the next one:

import java.util.*;


public class UserCollector {

    static List<User> allUsers;

    public static void addUserToTheList() {

        Scanner keyboardInput = new Scanner(System.in);

            System.out.println("Type nickname: ");
        String nickname = keyboardInput.nextLine();
            System.out.println("Type IP: ");
        String ipAddress = keyboardInput.nextLine();
            System.out.println("Type File Location: ");
        String fileLocation = keyboardInput.nextLine();

        System.out.println("User that is attempting to log in is: "+ nickname + " and his IP is: " + ipAddress);

        User inputUser = new User(nickname, ipAddress, fileLocation);

        allUsers = new ArrayList<User>();

        if (keyboardInput.nextLine().equalsIgnoreCase("INSERT") && !allUsers.contains(inputUser)) {

            allUsers.add(inputUser);
            System.out.println("User has been successfully added to your list.");
        }
        else
            System.out.println("This user already exists on the list!");

    }

    public static void currentStateOfTheList() {

        for (User u : allUsers) {
               System.out.println("nick: "+u.nickname +", ip: "+ u.ipAddress );
           }

    }

    public static void main(String[] args) {

        UserCollector.addUserToTheList();
        UserCollector.currentStateOfTheList();

    }

}

Now, the idea for the addUserToTheList() method is simple. Add objects of type User into the ArrayList. And also do so by typing nickname, ipAddress and fileLocation into the console. First time I ran it, it worked fine but it threw an Exception (NullPointer).
Now when I run it, it compiles fine but it says that I already have that user in the list although I always give different nickname/ipAddress/fileLocation.

I believe there is something wrong with User object that probably stays the same every time I try to run it.

I hope someone helps me. Thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T13:35:52+00:00Added an answer on May 13, 2026 at 1:35 pm

    Your program has a main with one call like this

     UserCollector.addUserToTheList();
    

    When the program completes, the list is destroyed. Next time you run, you get a new list. If your intent is to add lots of users then you either need to keep prompting for more users or you need to save the list you are building somewhere.

    You call

      allUsers = new ArrayList<User>();
    

    Every time in addUserToTheList, hence for each new user you will create a new list. You probably should initialise it in a constructor instead. But then you should not be using static methods. As I’ve advised you before, your main should

     UserCollector myCollector = new UserCollector();
    
     myCollector .addUserToTheList();
    

    The UserCollector constructor could initialise the user list

    public class UserCollector {
    
        private List<User> allUsers;
        public UserCollector() {
              allUsers = new ArrayList<User>();
        }
    

    then you don’t need static methods.

    Look at this:

        if (keyboardInput.nextLine().equalsIgnoreCase("INSERT") 
                  && !allUsers.contains(inputUser))    {
            allUsers.add(inputUser);
            System.out.println("User has been successfully added to your list.");
        }
        else
            System.out.println("This user already exists on the list!");
    

    When you type anything other than “INSERT” ypu with hit the “user already exists” method. I would always separate the clauses, give different messages.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 436k
  • Answers 436k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Now I want to be able to delete rows from… May 15, 2026 at 3:51 pm
  • Editorial Team
    Editorial Team added an answer The functions ff and gg in your example are the… May 15, 2026 at 3:51 pm
  • Editorial Team
    Editorial Team added an answer Something like: $('select.bla').change(function() { $value = $('select.bla').map(function(){return $(this).val()}).get().join(','); $('input.alloptions').val($value); });… May 15, 2026 at 3:51 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.