I think I am having a prolem that could be related to the scope of the variable. The code I am posting is part of a java file in a webapp. However my problem is not a typcial Jsp or Servlet problem, so I am posting it here.
This is my code:
What I intend the above code to do is to return the boolean value to the servlet that is calling this method. However I am getting an error that says..'Initialize variable bool'. I am not sure why I am getting this error. The assignment 'bool = true' is taking place within the if block, but the bool variable was declared outside of the if block, within the try block, so I thought when I do a 'return bool' it would return a value of true, as per the assignment, but it seems that's not the case. How do I overcome this? I hope someone can advise. Thanks.
You can't do like declaring local variable and assigning value to that local variable inside if condition. You must have to assign value to that local variable before using it and outside if condition.
Life is easy because we write the source code.....
Thanks for the reply. If I were to initialize bool with a value when I first declared it, than that value get's returned to the calling method, which is not what I want. A condition is to be met within the ifstatement and the bool value takes on a 'true' or 'false' based on the condition being met, it is this value that should be returned to the calling method. Anyway way to return the bool value within the if block when the condition is met? Really hope someone can help. Thanks.
If no record was found, what do you return ? True or false ?
Joined: Dec 23, 2008
Thanks for the reply. If no record was found the return should be false. I have modified the code as below:
When 'if(hoursused < allocatedhours)' returns true the servlet prints a 'pass' and a 'fail' if a false is returned. In the database I am setting the hoursused as less than allocatedhours, and yet I am getting a 'fail' as a result, which means the value returned must be false, even though the condition is met. I believe the assignment within the 'if' statement is somehow being ignored....but why??
Check the code. Can you see the way that makes it possible to reach and execute bool = falseafter executing bool = true?
Hint: try thinking through your algorithm - what is it that you want to achieve. I think you want to see if there exists a row where hoursused < allocatedhours.
BTW, why do you have 2 result sets (srs1, srs2)? As far as I can see, you exhaust (retrieve all rows from) srs2 while processing the 1st row from srs1; subsequent passes through the while(srs1.next()) loop will skip the inner while as srs2.next() will be false. This has nothing to do with your original problem, but it's still a serious bug.
Joined: Dec 23, 2008
Hi Istvan Kovacs ...
Thanks for chipping in, much appreciated. I am not sure if I understand what you are saying. Are you saying something is wrong with the way I coded the if-else statement? It seems fine to me, if there is indeed an error, could you point it out to me please.
Or are you refering to the while loops, I doubt if they are the problem cos I am looking at only one particular user whose 'hoursused' is fixed and so is his 'allocated hours'. There will not be a problem of the if condition being proven true once and false the next time, while looping as I am looking for only one particular user.
As for the 'return false' statement in the catch statement...if I dont place any return statement there I am getting the error: "missing error statement". Where am I going wrong??
Joined: May 06, 2010
Maybe your query only returns a single row in srs1 - but then why is it a while statement instead of an if?
The code, in my eyes, if quite messy. Maybe it's just me (no, it's not - http://www.javaranch.com/styleLong.jsp#return), but I don't like code with multiple exit points (such as return statements within control blocks, not at the end, except maybe some special cases (e.g. type and instance checks at the beginning of an equals method)).
Now back to your problem: OK, so srs1 will only have a single row. What about srs2?
It seems to me you're trying to do something like this:
retrieve some data into srs2 (not shown; BTW, this is not a meaningful variable name, the code is not self-documenting)
load a user via srs1 (again, a meaningless name)
check rows in srs2 to determine if there exists a row such that hoursUsed < allocatedHours (BTW, your variable name does not follow the generally accepted Java convention - camelCase)
However, what you actually do is:
retrieve some data into srs2 (what/how you load is not shown, but what happens if srs2 is empty?)
load a user (what happens if user does not exist and srs1 is empty?)
check the last row in srs2 and set bool = true if for that last rowhoursUsed < allocatedHours
You have to make sure that no matter what path is actually executed when you run the code:
bool is assigned a value (even if conditions you take for granted fail - such as there are no users, or not a single user)
you exit the method by returning a boolean value
Please note that we are encouraged to give hints, not solutions. It would have taken less time for me to produce a solution in Java than it took to write these hints. Please make sure you have found answers the questions posted; by the time you're done, you will have solved the problem. :-)
Just for a counter point of view, I have zero issue with multiple returns--when they make the code easier to understand. I don't *want* to have to keep looking through the code to see if anything needs to happen if nothing else does; that just wastes my time.In this case, the code you posted isn't clear in its intent, so it's really difficult to help much more than Istvan already has. I'd add that it's important to try to follow Java naming conventions--it makes code easier to read and understand (I don't like the braces like this, but that's a different issue).