在新行上输出WITHOUT for loop

时间:2016-03-09 00:59:55

标签: java

我正在尝试通过基本客户端服务器应用程序在新行上输出数组。但是,为了实现这一点,我不得不使用substring在每个单词之后找到#来表示该行的结尾。但是,我想删除此功能,并将每个部分放在一个新行上。

    public ClientHandler(Socket socket,Users newUser, int newClientUser)
                                                    throws IOException
{
    client = socket;
    input = new Scanner(client.getInputStream());
    output = new PrintWriter(
                    client.getOutputStream(),true);
    user = newUser;
    clientUser = newClientUser;

    String[] itemName = {user.getItemName(1), user.getItemName(2)};
    String[] description = {user.getItemDescription(1), user.getItemDescription(2)};
    String[] itemtime = {user.getItemTime(1), user.getItemTime(2)};

    output.println(itemName[0] + "@" + itemName[1] 
        + "@" + "Welcome To The Auction User:" + clientUser
         + itemName[0] +": "+ description[0] +
        "@"+ itemName[1] +": "+description[1]+
        "@"+ "Deadline For " + itemName[0] + ": " 
        + itemtime[0] + "#" +"Deadline For " + 
        itemName[1] + ": " + itemtime[1]+"@");

}

private synchronized void getMessage(String response) 
{
    String message="";
    for(int i= count; !response.substring(i, i+1).equals("@"); i++)
    {
       count = i;
    }
}

1 个答案:

答案 0 :(得分:0)

output.println(itemName[0] + "\n" + itemName[1] 
        + "\n" + "Welcome To The Auction User:" + clientUser
         + itemName[0] +": "+ description[0] +
        "\n"+ itemName[1] +": "+description[1]+
        "\n"+ "Deadline For " + itemName[0] + ": " 
        + itemtime[0] + "\n" +"Deadline For " + 
        itemName[1] + ": " + itemtime[1]+"\n");

不是使用“#”表示新行,而是使用“\ n”。 Java会将您的字符串中的内容读作新行。