We're a community of 1076K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,075,996 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

sum of 2 numbers jsp

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>
2
Contributors
4
Replies
1 Day
Discussion Span
1 Year Ago
Last Updated
5
Views
koricha
Newbie Poster
16 posts since Feb 2012
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

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

IIM
Practically a Master Poster
636 posts since Jun 2011
Reputation Points: 127
Solved Threads: 136
Skill Endorsements: 7

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

koricha
Newbie Poster
16 posts since Feb 2012
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

Never mind it works Thank you

koricha
Newbie Poster
16 posts since Feb 2012
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

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

IIM
Practically a Master Poster
636 posts since Jun 2011
Reputation Points: 127
Solved Threads: 136
Skill Endorsements: 7

This article has been dead for over three months: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
View similar articles that have also been tagged:
 
© 2013 DaniWeb® LLC
Page rendered in 0.0738 seconds using 2.75MB