简体   繁体   English

我将如何接受用户输入并将其添加到arraylist中?

[英]How would I take user input and add it into an arraylist?

I have currently written a class called PostTUI and I need to know if I could take the input people write from my scanner and add it into my arraylist. 目前,我已经编写了一个名为PostTUI的类,我想知道我是否可以接受人们从扫描仪中写入的输入并将其添加到我的arraylist中。

public class PostTUI {

    private Scanner scan = new Scanner(System.in);
    private PostManager manager;
    private String userChoice;
    private String author;
    private String message;

    public PostTUI(PostManager manager) {
        this.manager = new PostManager();
    }
    public void run() {
        System.out.println("1 - Create a new Message Post");
        System.out.println("2 - Create a new Photo Post");
        System.out.println("3 - Create a new Event Post");
        this.userChoice = this.scan.nextLine();

        if (this.userChoice.equals("1")) {
            System.out.println("Who is the author?");
            this.author = this.scan.nextLine();
            System.out.println("What is the message?");
            this.message = scan.nextLine();
            this.manager.addPost(Post newPost);
        }

    }
}

The PostManager class is the class who's job it is to declare and initialize the arraylist. PostManager类是声明和初始化arraylist的工作类。 It has a method that accepts and stores a post (addPost) and a toString method. 它具有接受和存储帖子(addPost)和toString方法的方法。

You haven't made it explicitly clear where you use an ArrayList , but presumably inside the PostManager and you add to it with the add() method. 您尚未明确说明使用ArrayList ,但大概是在PostManager内,并使用add()方法添加到其中。 In any case, that's not how you create a new object, which is why the last line is giving you an error message. 无论如何,这不是您创建新对象的方式,这就是为什么最后一行会向您显示错误消息的原因。 You want this instead: 您想要这个:

this.manager.addPost(new Post());

You haven't shown us the Post class, but maybe you want to pass the author and message String s as arguments to the Post constructor? 您尚未向我们展示Post类,但是也许您想将author和消息String作为参数传递给Post构造函数?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将用户输入添加到ArrayList? - How to add user input to ArrayList? 如何获取用户输入并在不同类中的数组列表中搜索用户输入? - How do I take a user input and search through an arraylist in a different class for the user input? 如何在Java中将类型为String的用户输入添加到ArrayList? - How do I add a User Input of type String to an ArrayList in Java? 如何获取用户输入并将其成功存储在 ArrayList 中? 那么我如何让我的程序向我显示 ArrayList 中的所有元素? - How do i take user input and store it successfully in an ArrayList? Then how do i get my program to show me all the elements in the ArrayList? 我想通过用户输入添加到arrayList - I want to add to an arrayList through user input 我无法弄清楚如何将用户输入放在逗号分隔的列表中并将其放入ArrayList中 - I cannot figure out how to take user input in a comma-separated list and place it into an ArrayList 为什么我不能将以下字符串作为输入并将其添加到ArrayList中? - Why am I not able to take the following string as input and add it to an ArrayList? 我如何获取用户输入的数字并单独处理其数字? - How would I take a user-input number and work on its digits individually? 如何将新数据添加到Arraylist中以及如何删除用户输入重复项? - How can I add new data to an Arraylist & How can I remove user input duplicates? 如何将我创建的对象添加到arraylist? - How would I add an object I created to an arraylist?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM