User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the JSP section within the Web Development category of DaniWeb, a massive community of 429,968 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,652 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our JSP advertiser: Lunarpages JSP Web Hosting
Views: 1524 | Replies: 3
Reply
Join Date: Dec 2006
Posts: 54
Reputation: staneja is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 1
staneja's Avatar
staneja staneja is offline Offline
Junior Poster in Training

Question variable Initialisation in JSP

  #1  
Mar 16th, 2007
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>
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Feb 2006
Posts: 1,469
Reputation: masijade is just really nice masijade is just really nice masijade is just really nice masijade is just really nice masijade is just really nice 
Rep Power: 9
Solved Threads: 131
masijade's Avatar
masijade masijade is offline Offline
Nearly a Posting Virtuoso

Re: variable Initialisation in JSP

  #2  
Mar 16th, 2007
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
Reply With Quote  
Join Date: Nov 2004
Location: Netherlands
Posts: 5,752
Reputation: jwenting is a jewel in the rough jwenting is a jewel in the rough jwenting is a jewel in the rough jwenting is a jewel in the rough 
Rep Power: 18
Solved Threads: 199
Colleague
jwenting's Avatar
jwenting jwenting is offline Offline
duckman

Re: variable Initialisation in JSP

  #3  
Mar 17th, 2007
and why do this at all?
When will kids learn that scriptlets are there for backwards compatibility only and should never have been invented?
As people are clearly allowed to attack me but I'm not allowed to defend myself, I no longer post to this site.
Reply With Quote  
Join Date: Dec 2006
Posts: 54
Reputation: staneja is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 1
staneja's Avatar
staneja staneja is offline Offline
Junior Poster in Training

Re: variable Initialisation in JSP

  #4  
Mar 19th, 2007
Thanks a lot for the answer.It really makes sense to me and i didnt thought about last nested loop.

Thanks once again
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb JSP Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the JSP Forum

All times are GMT -4. The time now is 11:47 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC