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>

Recommended Answers

All 4 Replies

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.
.

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

Never mind it works Thank you

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.