aspose file tools*
The moose likes Beginning Java and the fly likes Initialize variable bool Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Initialize variable bool" Watch "Initialize variable bool" New topic
Author

Initialize variable bool

Varnam Aayiram
Ranch Hand

Joined: Dec 23, 2008
Posts: 88
Hi Guys...

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.
Pramod P Deore
Ranch Hand

Joined: Jul 15, 2008
Posts: 629

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.....
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14688
    
  16

The automatic initialization is only available to instance variables. You must initialize local variables. "boolean bool" is a local variable, and you have to initialize it to either true, or false.


[My Blog]
All roads lead to JavaRanch
Varnam Aayiram
Ranch Hand

Joined: Dec 23, 2008
Posts: 88
Hi Guys...

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.


Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14688
    
  16

If no record was found, what do you return ? True or false ?
Varnam Aayiram
Ranch Hand

Joined: Dec 23, 2008
Posts: 88
Hi Chris...

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??
Istvan Kovacs
Ranch Hand

Joined: May 06, 2010
Posts: 100
Check the code. Can you see the way that makes it possible to reach and execute bool = false after 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.


Varnam Aayiram
Ranch Hand

Joined: Dec 23, 2008
Posts: 88
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??

Istvan Kovacs
Ranch Hand

Joined: May 06, 2010
Posts: 100
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 row hoursUsed < 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. :-)
    David Newton
    Author
    Rancher

    Joined: Sep 29, 2008
    Posts: 12617

    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).
    Campbell Ritchie
    Sheriff

    Joined: Oct 13, 2005
    Posts: 39828
        
      28
    Try adding && !bool to the condition for your loop. You will of course have to initialise it to false beforehand. And try a better name, like "found", for that variable.
     
    wood burning stoves
     
    subject: Initialize variable bool