views:

61

answers:

2

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

A: 

I wonder if you are having problems because you have two differnt things trying to get at the System.in object. You have a Scanner in your User class asking for System.in, and you have a scanner in your other class asking for System.in. How does the console know which object to pass your input to?

This might not be your problem, but you may want to redesign your classes so that only one is accepting user input from the command line.

darren
+2  A: 

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.

djna
OK, I'm gonna listen to you completely this time and will inform you here about eventual changes. Thank you again :)
AmateurProgrammer
One more question, what should I put into constructor? I'm not completely clear with this
AmateurProgrammer
I've amended the answer to show the constructor.
djna