2014-03-05 97 views
0

我目前寫了一個名爲PostTUI的類,我需要知道我是否可以將輸入的人從我的掃描儀寫入並將其添加到我的數組列表中。我如何接受用戶輸入並將其添加到數組列表中?

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

    } 
} 

PostManager類是聲明和初始化數組列表的工作。它有一個方法來接受並存儲一個post(addPost)和一個toString方法。

+0

最後一行代碼沒有工作,所以我只是假設我做了一些非常錯誤的事情。 – user3241721

回答

1

您還沒有明確說明您在哪裏使用ArrayList,但推測是在PostManager之內,並且您使用add()方法添加了該方法。無論如何,這不是你如何創建一個新的對象,這就是爲什麼最後一行給你一個錯誤信息。你想這個代替:

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

您還沒有我們顯示的Post類,但也許你想給作者和消息String S作爲參數傳遞給Post構造?

+0

我對繼承自post的每個消息,照片和事件發佈類都有繼承。我需要做的就是除了將Post()更改爲this.manager.addPost(新的MessagePost(作者,消息))之外,你所說的內容。它的工作。謝啦! – user3241721

相關問題