我的 Java 类 addUserToTheList() 不起作用

发布于 2024-08-17 18:01:25 字数 2815 浏览 2 评论 0原文

我有以下两个类:

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;
    }



}

和下一个:

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();

    }

}

现在, addUserToTheList() 方法的想法很简单。将 User 类型的对象添加到 ArrayList 中。还可以通过在控制台中输入昵称、ip 地址和文件位置来完成此操作。我第一次运行它时,它运行良好,但抛出了异常(空指针)。 现在,当我运行它时,它编译得很好,但它说我已经在列表中拥有该用户,尽管我总是给出不同的昵称/ipAddress/fileLocation。

我相信 User 对象有问题,每次我尝试运行它时它可能都保持不变。

我希望有人帮助我。谢谢

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

安人多梦 2024-08-24 18:01:25

你的程序有一个 main 和一个像这样的调用

 UserCollector.addUserToTheList();

当程序完成时,列表被销毁。下次运行时,您会得到一个新列表。如果您的目的是添加大量用户,那么您要么需要不断提示更多用户,要么需要将正在构建的列表保存在某处。

  allUsers = new ArrayList<User>();

每次都调用 addUserToTheList,因此对于每个新用户,您都将创建一个新列表。您可能应该在构造函数中初始化它。但是你不应该使用静态方法。正如我之前建议您的那样,您的 main 应该

 UserCollector myCollector = new UserCollector();

 myCollector .addUserToTheList();

UserCollector 构造函数可以初始化用户列表

public class UserCollector {

    private List<User> allUsers;
    public UserCollector() {
          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!");

当您键入“INSERT”以外的任何内容时,请点击“用户已存在”方法。我总是会把这些条款分开,给出不同的信息。

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.

虫児飞 2024-08-24 18:01:25

我想知道您是否遇到问题,因为您有两个不同的东西试图获取 System.in 对象。您的 User 类中有一个 Scanner 要求 System.in,并且您的其他类中有一个 Scanner 要求 System.in。控制台如何知道将输入传递给哪个对象?

这可能不是您的问题,但您可能想要重新设计您的类,以便只有一个类接受来自命令行的用户输入。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文