Nick
Nick

Reputation: 597

Can single String contain multiple lines?

For example if I assign multiple lines to a string as so:

while ((line = reader.readLine()) != null)
        {
            output += line + "\n";
        }

Is it possible for me to return output with line separators as one String?

I'm writing a Socket program that has Client and Server program, where Client sends request to Server and server returns that request in form of a String back to Client, but some String are multiple lines.

Server Program code (part of code):

if (clinetChoice.equals("3"))
    {
        String command = "free";

        Process process = Runtime.getRuntime().exec(command);

        BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));

        System.out.println("You Chose Option Three");

        String line;            

        while ((line = reader.readLine()) != null)
        {
            output += line;
            System.out.println(line);
            line = reader.readLine();
        }

    }

Client Program code:

while ((fromServer = input.readLine())+"\n" != null)
    {
        System.out.println("Server: " + fromServer);            
        if (fromServer.equals("Bye"))
            break;          

        System.out.print("Enter your choice: ");
        fromClient = stdIn.readLine().trim();

        if(fromClient.equals("1"))
        {
            System.out.println("Client: " + fromClient);
            output.println(fromClient);

        }

fromServer in Client program is output from Server program. This works fine for output that's one line, but if its multiple lines I can't figure out how to print it all at once.

So if output for example equals:

One
Two
Three
Four

It returns as this:

One
Enter your choice:  (It prompts me for new command)
Two
Enter your choice:
Three
Enter your choice:
Four

So it basically prints one line, ask me for new choice and doesn't matter what I enter it prints second line, then third line and so forth until it reaches last line, instead of printing like this:

One
Two
Three
Four
Enter your choice:

Upvotes: 0

Views: 2272

Answers (2)

Niraj Nawanit
Niraj Nawanit

Reputation: 2451

There is another mistake in code: while ((fromServer = input.readLine())+"\n" != null). It will be always true. You should only check: while ((fromServer = input.readLine()) != null).

Also if I understand your requirements correctly, your code should be something like below:

String fromServer = "";
String line;
while ((line = input.readLine()) != null) {
    fromServer += line + "\n"; // collect multiline strings into fromServer
}

System.out.println("Server: " + fromServer);            
if (fromServer.equals("Bye"))
    break;          

System.out.print("Enter your choice: ");
fromClient = stdIn.readLine().trim();

if(fromClient.equals("1"))
{
    System.out.println("Client: " + fromClient);
    output.println(fromClient);
}

Upvotes: 1

Jayson
Jayson

Reputation: 950

why not just move this to outside the while loop?

    System.out.print("Enter your choice: ");
    fromClient = stdIn.readLine().trim();

Upvotes: 0

Related Questions