Results 1 to 2 of 2
  1. #1
    angie13 is offline Member
    Join Date
    Sep 2010
    Posts
    1
    Rep Power
    0

    Default Pesky errors in debugging

    I'm an online student, am now taking my second Java course and I'm often stumped. I've copied my files here for an application that is a calculator where results are displayed from user input. This is my first time working with Java, JSP and HTML at the same time. Can anyone tell me why this application won't run? The first page works alright, but when it goes to the results page it dies while trying to calculate the results. Help!

    Form page:
    <!doctype html public "=//W3C/DTD HTML 4.U Transitional//EN">
    <html>
    <head>
    <title>Simple Calculator</title>
    </head>

    <body>

    <h1>Simple Calculator</h1>

    <form action="lab02Results.jsp" method="get">

    <table cellspacing="5">
    <tr>
    <td align="right">Enter a Number:</td>
    <td><input type="text"name="number1">
    </td>
    </tr>
    <tr>
    <td align="right">Enter Another Number:</td>
    <td><input type="text"name="number2">
    </td>
    </tr>
    <tr>
    <td align="right">Select an Operator:</td>
    <td>
    <select name="operator">
    <option value="add">add</option>
    <option value="subtract">subtract</option>
    <option value="multiply">multiply</option>
    <option value="divide">divide</option>
    </select>
    </td>
    </tr>

    <tr>
    <td align="right">Result: </td>
    <td><input type="text"name="result">
    </td>
    </tr>

    <tr>
    <td></td>
    <td><input type="submit" value="Compute">
    </td>
    </tr>
    </table>
    </form>

    <form method="link" action="lab02Form.jsp">
    <input type="submit" value="Reset">
    </form>


    </body>

    </html>


    Results page:
    <!doctype html public "=//W3C/DTD HTML 4.U Transitional//EN")
    <html>
    <head>
    <title>Simple Calculator</title>
    </head>

    <%@ page import="java.lang.Double" %>
    <%@ page language="java" errorPage="" %>

    <body>

    <%
    String num1=request.getParameter("number1");
    String num2=request.getParameter("number2");
    String operator=request.getParameter("operator");

    Double.parseDouble(num1);
    Double.parseDouble(num2);
    %>

    <h1>Simple Calculator</h1>

    <%! public static double getResult(Double num1, Double num2, String operator)
    {

    if(operator.equals("add")) {
    double result=num1+num2;
    } else if(operator.equals("subtract")) {
    double result=num1-num2;
    } else if(operator.equals("multiply")) {
    double result=num1*num2;
    } else {
    double result=num1/num2;
    }
    }
    %>


    <form action="lab02Form.jsp" method="get">

    <table cellspacing="5">
    <tr>
    <td align="right">Enter a Number: </td>
    <td><input type="text"name="num1" value="num1">
    </td>
    </tr>
    <tr>
    <td align="right">Enter Another Number: </td>
    <td><input type="text"name="num2" value="num2">
    </td>
    </tr>
    <tr>
    <td align="right">Select an Operator: </td>
    <td>
    <select>
    <option value="add">add</option>
    <option value="subtract">subtract</option>
    <option value="multiply">multiply</option>
    <option value="divide">divide</option>
    </select>
    </td>
    </tr>

    <tr>
    <td align="right">Result: </td>
    <td><input type="text"name="result" value="<%=result%">>
    </td>
    </tr>

    <tr>
    <td></td>
    <td><input type="submit" value="Reset">
    </td>

    <td></td>
    <td><input type="submit" value="Compute">
    </td>
    </tr>
    </table>


    </body>

    </html>

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,846
    Rep Power
    19

    Default

    "it dies"?
    That could mean anything.
    Does it throw an exception?

    ...and just for traditions sake, I shall now complain about courses getting people to write JSPs before they have learned about Servlets. All that code should be in a Servlet, or called by a servlet preferably, and the result passed on to the JSP. JSPs are for display.

Similar Threads

  1. help debugging
    By mluu510 in forum New To Java
    Replies: 3
    Last Post: 08-21-2010, 01:28 PM
  2. Debugging Help Needed
    By vittoire in forum New To Java
    Replies: 3
    Last Post: 01-26-2010, 05:31 PM
  3. Debugging
    By daro in forum Eclipse
    Replies: 0
    Last Post: 07-22-2009, 05:02 PM
  4. Replies: 3
    Last Post: 03-08-2009, 01:51 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •