Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Trying to debug this java program it is a simple servlet to calculate the area o

ID: 645147 • Letter: T

Question

Trying to debug this java program it is a simple servlet to calculate the area of a rectangle based on two doubles, length and width, containing to small errors of the same type.

import java.text.*;

import java.io.IOException;

import java.io.PrintWriter;

import javax.servlet.ServletException;

import javax.servlet.annotation.WebServlet;

import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

@WebServlet(name = "FindArea", urlPatterns = {"/FindArea"})

public class FindArea extends HttpServlet {

    private final String target = "localhost:7070";

    

    @Override

    protected void doGet(HttpServletRequest request, HttpServletResponse response)

            throws ServletException, IOException {

        response.setContentType("text/html;charset=UTF-8");

        PrintWriter out = response.getWriter();

        printHeader(out);

        printForm(out);

        printFooter(out);

    }

    @Override

    protected void doPost(HttpServletRequest request, HttpServletResponse response)

            throws ServletException, IOException {

        String strLngth = request.getParameter("length");

        String strWdth = request.getParameter("width");

        double lngth = Double.valueOf(strLngth);

        double wdth = Double.valueOf(strWdth);

        double area = wdth * lngth;

        String strArea = formatDouble(area);

        PrintWriter out = response.getWriter();

        printHeader(out);

        printForm(out);

        out.println("<br>");

        out.println("<h3>The area is: " + strArea + "</h3>");

        printFooter(out);

    }

    public void printHeader(PrintWriter out) {

        out.println("<html>");

        out.println("<head>");

        out.println("<title>");

        out.println("<Find the Area>");       

        out.println("</title>");           

        out.println("</head>");    

        out.println("<body>");

        out.println("<h2>Find the Area</h2>");

    }

    

    public void printForm(PrintWriter out) {   

            out.println("<form method='post' action='http://" + target +

                    "/Week5Debug/FindArea'>");

            out.println("<label>Length:");

            out.println("<input type='text' name='Length' size='5' " +

                    "maxlength='5' /></label><br><br>");

            out.println("<label>Width: ");

            out.println("<input type='text' name='Width' size='5' " +

                    "maxlength='5' /></label><br><br>");

            out.println("<input type='submit' />");

            out.println("<input type='reset' />");

            out.println("</form>");

    }    

    

    public void printFooter(PrintWriter out) {

        out.println("</body>");            

        out.println("</html>");

    }

    

    public static String formatDouble(double value ) {

      DecimalFormat myFormatter = new DecimalFormat("###,###.##");

      String output = myFormatter.format(value);

      return output;

      }

}

Explanation / Answer

Here is the error is in formatDouble(double value ) method

String output = myFormatter.format(value);

In this statement value is assigned to a string variable, thus it gives error.

you will have conert the variable output in string and then return it.

like

String output = String.valueOf(myFormatter.format(value));