Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8879723
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T20:00:50+00:00 2026-06-14T20:00:50+00:00

I am writing a small program with a Client and a Server. The server

  • 0

I am writing a small program with a Client and a Server. The server program contains the calculator program in it. While the server and the client runs, the client can input and equation eg. 2 + 2 and the server will calculate it and send the answer to the client. The program should contain a help option as well so when the command help is sent from the client to the server, the server should return a help menu.
My problem is that it is returning the help menu but all in one line, it is not printing it out to the console in separate lines. Thanks for any help.

SERVER CODE:

package One;

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


    public class ServerCalculator {

        static double answer;

        public ServerCalculator(){

            System.out.println("Server...");

            theServer();

            }
        void theServer(){

            try{

                ServerSocket ss = new ServerSocket(9990);
                while(true){

                    Socket sc = ss.accept();
                    Help hp = new Help();
                    BufferedReader br = new BufferedReader(new InputStreamReader(sc.getInputStream()));
                    String compute = br.readLine();

                    Maths(compute);
                    PrintWriter pw = new PrintWriter(sc.getOutputStream());

                    if(compute.equals("help")){
    //                  pw.println(hp.noOfLines());
                        pw.println(hp.menu());
                    }

                    if(compute.equals("exit")){
                        ss.close();
                    }

                    else{
                        pw.println(answer); 
                    }

                    pw.flush();
                }

            }

            catch (Exception ee){
                ee.printStackTrace();
            }

        }

        static double Maths(String compute){

          String message[] = compute.split(" ");


          if(message[0].equalsIgnoreCase("Help"))
          {
              return -2;
          }

          if(message[0].equalsIgnoreCase("Exit"))
          {
              return -1;
          }


          double rad = 0;

          double a = Integer.parseInt(message[0]);
          double b = Integer.parseInt(message[2]);

          if(message[1].equalsIgnoreCase("Sin")){
                rad = Math.toRadians(a);
                answer = Math.sin(rad);
            }
            if(message[1].equalsIgnoreCase("Cos")){
                if(a==90 || a==270){
                    answer = 0;
                }
                else{
                    rad = Math.toRadians(a);
                    answer = Math.cos(rad);
                }
            }
            if(message[1].equalsIgnoreCase("Tan")){
                if(a==90 || a==270){
                    System.out.println("Invalid Calculation");
                    answer =  0;
                }
                else if(a==45 || a==135){
                    rad = Math.toRadians(a);
                    answer = Math.tan(rad);
                }
                rad = Math.toRadians(a);
                answer = Math.tan(rad);
            }
        if(message[1].equalsIgnoreCase("Power") || (message[1].equalsIgnoreCase("^")))
        {
            answer = Math.pow(a, b);  
        }

          if(message[1].equalsIgnoreCase("Multiply") || (message[1].equalsIgnoreCase("*")))
          {
              answer = a*b;  
          }
          if(message[1].equalsIgnoreCase("Add") || (message[1].equalsIgnoreCase("+")))
          {
            answer = a+b;
          }
          if(message[1].equalsIgnoreCase("Subtract") || (message[1].equalsIgnoreCase("-")))
          {
            answer = a-b;
          }
          if(message[1].equalsIgnoreCase("Divide") || (message[1].equalsIgnoreCase("/")))
          {
            answer = a/b;
          }
            return answer;

      }


    }

CLIENT CODE:

package One;

import java.net.*;
import java.util.Scanner;
import java.io.*;

    public class ClientCalculator {


        public static void main(String[] args) {

            Socket sc;  

            System.out.println("Client...");

            while(true){

                try
                {
                    Scanner sin = new  Scanner(System.in);
                    String question = sin.nextLine(); 
                    System.out.println("Processing: " + question);


                    sc = new Socket("localhost",9990);

                    PrintWriter pw = new PrintWriter(sc.getOutputStream());

                    pw.println(question);   
                    pw.flush();

                    BufferedReader br = new BufferedReader(new InputStreamReader(sc.getInputStream()));
                    String answer = br.readLine();
                    System.out.println(question + " = " + answer);
//                  sc.close();
//                  sin.close();
                }

                catch(Exception ee){

                }
            }
     }



    }

HELP MENU:

package One;

public class Help {

String menu(){

    String helpMenu = 
    "Instructions for the calculator " +
    "Input the number followed by space and then by word or operator and by number to get result " +
    "e.g. 5 + 5. or 30 Sin 30 - where 30 is the angle. " + 
    "Options are " + 
    "Multiply or (*) " + 
    "Add or (+) " +  
    "Subtract or (-) " + 
    "Divide or (/) " + 
    "Sin, Cos, Tan ";

    return helpMenu;

}


//  String noOfLines() {
//      
//      return "9";
//  }

}

RUN CODE:

package One;

public class TestClass {

    public static void main(String[] args) {

        new ServerCalculator();

    }

}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T20:00:52+00:00Added an answer on June 14, 2026 at 8:00 pm

    You have to add a while loop around your readline method in the client:

    String answer = null;
    while((answer=br.readLine()) != null)
    {
    //print answer
    }
    

    it is also preferred to use constants instead of hardcoding it: System.getProperty("line.separator");

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a small Java RMI Server and Client program I'm writing. I have
I'm writing a small program in C that will read input from the console.
I'm writing a small program which will make a GET request to a server
I'm writing a small program to calculate a physics problem but I'm having problems
I'm running a multithreaded socket program with valgrind. The client will send out a
I'm writing a small program (a twitter client) in Java, aimed at Mac OS
I'm writing a small program, it can show and manager all files and folder
I am writing a small java program that can measure the speed of my
I am writing 2 small programs (a server and a client) and whenever I
I am writing a small program to send file between Android and PC through

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.