0

I am new to JSP and was trying a very simple program that adds/Divide/multiply/Subtract 2 numbers
I am taking two numbers from a web page and as per the button pressed performing the action on JSP page. I have kept a vraiable c for output

If i write int c=0 ;then it works fine but if i only write int c and then code its not working
I have never observed this behavior in core Java.Is variable initialisation is mandatory in JSP.FOllowing is code i am using that throws exception

<body><% 
            int a=Integer.parseInt(request.getParameter("t1"));
            int b=Integer.parseInt(request.getParameter("t2"));
            int c ;
            String label=request.getParameter("b1");
            if (label.equals("add"))
                c=a+b;
            else
                if (label.equals("sub"))
                c=a-b;
                else
                    if (label.equals("mul"))
                    c=a*b;
                    else
                        if (label.equals("div"))
                        c=a/b;
        %>
         <% out.print(c);%>
    </body>
3
Contributors
3
Replies
4
Views
10 Years
Discussion Span
Last Post by staneja
0

What happens, if it makes it all the way to the last nested if statement and that statement evaluates to false instead of true? What value would c have then? None because it hasn't been initialised. Add an else to the last nested if and the error will go away.

0

and why do this at all?
When will kids learn that scriptlets are there for backwards compatibility only and should never have been invented?

0

Thanks a lot for the answer.It really makes sense to me and i didnt thought about last nested loop.

Thanks once again

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.