Java: Returning a String that contains line feed

167 Views Asked by At

I have an ArrayList<Group> listOfGroups.

The groups have 4 fields - String groupName, int lastActiveID, int firstID, String indicator. I want to write a method that returns info about all the groups in listOfGroups.

Here's what I'm trying:

String theList="";
for(Group gr:listOfGroups){
     theList+=gr.groupName+" "+gr.lastActiveID+" "+gr.firstID+" "+gr.indicator+"\n";
}
System.out.print(theList);
 return theList;

When I invoke the method, I only get the info about the first group, second invocation returns info about the second group and so on...Is it because of the \n character?

I put this System.out.println(theList) just to see what the string contains and I get the info about all the groups. That's exactly what I want on return.

How can I fix it?

EDIT: It's supposed to be a server and a client. This is my server:

package server;

import java.io.*;
import java.net.*;


public class Server {
    public static void main(String[] args){
        if(args.length!=1){
            System.err.println("Usage: java Server <port number>");
            System.exit(1);
    }

    int portNumber=Integer.parseInt(args[0]);

    try(
            ServerSocket serverSocket=new ServerSocket(portNumber);
            Socket clientSocket=serverSocket.accept();
            PrintWriter out=new PrintWriter(clientSocket.getOutputStream(),
                    true);
            BufferedReader in=new BufferedReader(
                    new InputStreamReader(clientSocket.getInputStream()));
    ){
        String command;
        String response;
        NNTPProtocol protocol=new NNTPProtocol();
        while((command=in.readLine())!=null){
            response=protocol.processCommand(command);
            out.println(response);
        }

    }
    catch(IOException e){
        e.getMessage();
    }
}
}

Here is the NNTPProtocol:

package server;

import java.io.*;
import java.util.ArrayList;


public class NNTPProtocol {

ArrayList<Group> listOfGroups=new ArrayList<Group>();
Iterator it=listOfGroups.iterator();

static int curPos=0;



String processCommand(String command){
    if(command.equalsIgnoreCase("list")){
        return getListOfGroups();

    }
    else return null;
}

String getListOfGroups(){
    if(listOfGroups.isEmpty()){
        try{
            DataInputStream in=new DataInputStream(new BufferedInputStream(
                    new FileInputStream(
                    "C:\\Users\\Ivo\\Documents\\NetBeansProjects\\"
                    + "NNTPServerClient\\GroupsInfo.txt")));
            String groupName;
            while(!(groupName=in.readUTF()).equals("end")){
                listOfGroups.add(new Group(groupName, in.readInt(),
                        in.readInt(), in.readUTF()));
            }

        }catch(FileNotFoundException e){}
        catch(IOException e){}
    }
    String theList="";
    for(Group gr:listOfGroups){
        theList+=(gr.groupName+" "+gr.lastActiveID+" "+
                gr.firstID+" "+gr.indicator+"\n");
    }
    System.out.print(theList);
    return theList;
}
}

And the Group class:

package server;

public class Group {
String groupName;
String indicator;
int firstID;
int lastActiveID;
Group(String groupName, int firstID, int lastActiveID, String indicator){
    this.groupName=groupName;
    this.indicator=indicator;
    this.firstID=firstID;
    this.lastActiveID=lastActiveID;
}
}
1

There are 1 best solutions below

1
On

you can Override toString method in Group class like this

public String toString() {
    return "groupName+" "+lastActiveID+" "+firstID+" "+indicator+"\n";
}

and if you want to retrieve theList you can use just

listOfGroups.toString();