0

i am trying to get the sum of 2 number.. it should give me a message if the sum is correct. i get an error "21 in the jsp file: /index.jsp
Incompatible operand types String and int" .any suggestions whats wrong with the program.

<%@ page import="java.io.*"%><%@
page import="java.util.*"%><?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
<%
        String sum = (String) session.getAttribute("sum");
        if (sum==null) {
                sum = "0";
                session.setAttribute("sum",sum);
        }
        int isum = Integer.parseInt(sum);
        String fib1 = "8";
        int ifib1 = Integer.parseInt(fib1);
        String fib2 = "13";
        int ifib2 = Integer.parseInt(fib2);
        isum=ifib1+ifib2;

        if(request.getParameter("number")==isum)
        {
                if(request.getParameter("submit") != null){
                out.print("correct");
                }
        }
%>
<body>
        <%=fib1%> + <%=fib2%> = <input type="text" name="number">
        <input type="button" value="go" onclick="doTheGObutton();">
        <form action="index.jsp" method="POST">
        <input type="submit" value="continue">
        </form>

</body>
</html>

Edited by peter_budo: Moving to JSP section

2
Contributors
4
Replies
5
Views
5 Years
Discussion Span
Last Post by IIM
1

if(request.getParameter("number")==isum)

Problem is in this line(line 20)
Replace it by this

    if(request.getParameter("number")!=null && Integer.parseInt(request.getParameter("number"))==isum)

Also make the following change in the body tag

<body>

        <input type="button" value="go" onclick="doTheGObutton();">
        <form action="index.jsp" method="post">
          <%=fib1%> + <%=fib2%> = <input type="text" name="number">
        <input type="submit" value="continue" name="submit">
        </form>
</body>

This is required as the input text "number" is outside form so will not be submitted and write the name of submit button as "submit" as you are checking that in your code.
.

0

Thank you IIM but for some reason it doesnt print oput "Correct"

0

I have checked in my system and it is printing "correct".Hopefully you made all the changes correctly.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.