1

I have one program that basically returns a String that can be sometimes one line (which works fine) but other times it can be multiple lines which causes me problems. Basically I have user choose an option that gets sent to second program and based on that option it returns a certain String. However if I put user selection outside my loop it works fine if String returned is multiple lines, but I need it to keep prompting users with options menu until they choose to quit. Here is the code for that:

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

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


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

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

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

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

        }


    }

This as I mentioned above returns multi-line String fine, but it doesn't prompt user anymore for option selection. If I move the user selection inside the loop and multi-line String gets returned it prints one line, asks user for their selection, prints second line, asks user for their selection, prints third line, asks user for their selection...

1
  • So if I understand it correctly, sometimes the response from the server is one line, sometimes multiple lines. Is there something that marks the end of a server response? Commented Sep 11, 2012 at 17:16

2 Answers 2

1

The code is fairly hard to follow. More context would be helpful. What is "input"? (i.e. fromServer = input.readLine())

Anyway, regardless of how many lines your server sends, you're only reading 1 at a time before prompting for user input. Try handling all of the server's response before sending your client's command. For example:

boolean shouldExit = false;

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

if (shouldExit) System.exit(0)  // Or whatever you want to do

//Now handle sending your client's command to the server.
//...
Sign up to request clarification or add additional context in comments.

6 Comments

The input comes from user in for of an integer like: 1, 2, 3, or 4. That response gets sent to Server program that based on that selection from user returns appropriate response. All of these responses are one liner except option 3 which returns multiple lines that is causing me problems. Problem for me is as I've mentioned I need client program to print out the server response out all at once and then prompt user for option selection again, but like you mentioned I only read one line at a time back. I'm trying to figure out how I can read back all of those line and prompt user again.
This answer should solve your problem then. I assume "input" (fromServer = input.readLine()) is some kind of buffer?
Yep it comes from a buffer. Also can you explain your code little more in detail? I'm confused as to how I can include user selection part.
But I need client's command to run in a loop, that's the whole point.
Well I've played around with the code you provided, no luck. Is there really no way to do this?
|
0

If you want to communicate client and server multiple times you have to make two loops.

  1. First loop will ask user for selection, launch second loop and do anything you want based on server response and user selection
  2. Second loop will read all lines from server

It may look like that:

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

    StringBuilder responseSb = new StringBuilder();
    while( ( fromServer = input.readLine() ) != null ) {
        responseSb.append( fromServer );
        responseSb.append( "\n" );
    }

    String response = responseSb.toString().trim();

    System.out.println( "Server: " + response );
    if( response.equals("Bye") ) {
        break;
    }


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

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

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

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

    }


}

1 Comment

Hey michael thanks for you response. I've tried using your code and when I run it, it prompts for user selection and after I type it in it doesn't do anything else.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.