English 中文(简体)
my Java class addUserToTheList() doesn t work
原标题:

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

最佳回答

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.

问题回答

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.





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签