Please support our JSP advertiser: Lunarpages JSP Web Hosting
![]() |
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
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> 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.
Java Programmer and Sun Systems Administrator
----------------------------------------------
Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
--Brian Kernighan
----------------------------------------------
Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
--Brian Kernighan
•
•
Join Date: Nov 2004
Location: Netherlands
Posts: 5,749
Reputation:
Rep Power: 19
Solved Threads: 200
![]() |
Similar Threads
Other Threads in the JSP Forum
- Passing variable value from javascript to jsp page at run time (JSP)
- javascript variable in jsp session (JSP)
- How to store javascript variable in jsp session (JSP)
Other Threads in the JSP Forum
- Previous Thread: Auto call to a servlet file inside Tomcat 4.1
- Next Thread: JSP learning process
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)






Linear Mode