java - 字符串不添加到列表 - Java

标签 java

我遇到一个问题,我将字符串变量发送到服务器、从服务器、主题、消息和附件到服务器,当我将它们放在列表中时,消息变量始终为空!我已经输出了变量消息,它给出了它应该做的但是,但是一旦我把它放在列表中。它显示为空。

private void doSend(String name)
{
    String to = input.nextLine();
    String from = input.nextLine();
    String subject = input.nextLine();
    String message = input.nextLine();
    String attachment = input.nextLine();        

    System.out.println(to);
    System.out.println(from);
    System.out.println(subject);
    System.out.println(message);
    System.out.println(attachment);        

    // stores the message, but not into the mailbox        
    MultiEchoServer.MailBox.add(new Email(to, from,subject, message, attachment));

    System.out.println(MailBox);

    System.out.println("Message Sent to: " + to);
    System.out.println(message);
}

样本输出

pj     // this is the to variable

dsds   // this is the from variable

subject  // this is the subject variable

message  // this is the message variable

[pj dsds subject null]  //this is the Mailbox List

Message Sent to: pj //not part of the error

message // this is the message variable being outputted again to see it it changed

我什至不确定是否有人可以帮助我,但如果您需要查看更多代码,请告诉我,谢谢!

电子邮件类

class Email
{
    private String to, from, subject,  message, attachment;
    int id;        

    public Email(String to ,String from ,String subject, String message, String attachment)
    {
        this.to = to;
        this.from = from;
        this.subject = subject;
        this.message = message;
        this.message = attachment;
    }

    public int id()
    {   
        return(id);
    }

    public String to()
    {
        return(to);
    }

    public String from()
    {   
        return(from);
    }

    public String subject()
    {
        return(subject);
    }

    public String message()
    {
        return(message);
    }
    public String attachment()
    {
        return(attachment);
    }

    public String toString()
    {
        return(to + " " + from + " " + subject + " " + message + "" + attachment);
    }
}

最佳答案

Email构造函数 有问题。您使用 messageattachment 两次分配 message 字段。

关于java - 字符串不添加到列表 - Java,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29868407/

相关文章:

java - 枚举二进制数据的 DFDL 解码

java - JFrame 编号输入

java - 如何在oracle数据库11g中添加sql文件?

java - 如何在 JSP 中导入我的导入?

java - 如何使用 GUI intelliJ 显示框架

java - 格式化日志的正确方法

java - Eclipse 工作区是否依赖于特定的 Eclipse 版本?

java - 如何使用java获取转发域的IP地址

java - 如果用户输入不是字符串,如何引发异常?

c# - 适合在沙箱中运行客户端代码的语言