aspose file tools*
The moose likes Beginning Java and the fly likes Error incrementing value in catch block Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Error incrementing value in catch block" Watch "Error incrementing value in catch block" New topic
Author

Error incrementing value in catch block

S Gregg
Greenhorn

Joined: Oct 13, 2009
Posts: 15
Hey guys can someone please help me to figure out why my error variable refuses to increment in my catch block. I need it to increment because I need to give the user 2 chances to enter the correct code and if they do not then the program calculates the values it has and quits. In it's current form, it runs fine and will calculate the value and will throw the exception and handle it accordingly except for the error variable. Due to this when i make this change to my while condition,

while (error < 3 || continueLoop)

to check for the number of errors and if I need to exit the loop or not, the program gets stuck in an infinite loop. Here is the code I have.

Lorand Komaromi
Ranch Hand

Joined: Oct 08, 2009
Posts: 276
error < 3 && continueLoop


OCJP 6 (93%)
S Gregg
Greenhorn

Joined: Oct 13, 2009
Posts: 15
Lorand Komaromi wrote:error < 3 && continueLoop


Wouldn't this mean that both conditions have to be false for the loop to exit. I want to exit if only one of them is false. This still doesn't help with the error variable not incrementing though
John de Michele
Rancher

Joined: Mar 09, 2009
Posts: 600
'S':

No, logical AND (&&) will fail if either the left or right side is false. AND is only true if both sides are true.

John.
S Gregg
Greenhorn

Joined: Oct 13, 2009
Posts: 15
John de Michele wrote:'S':

No, logical AND (&&) will fail if either the left or right side is false. AND is only true if both sides are true.

John.


But I want it to fail if one or the other is false. If you dont enter the right input the loop should end or if the program runs all the way to the end (which is the continueLoop = false) the loop should exit
John de Michele
Rancher

Joined: Mar 09, 2009
Posts: 600
'S':

That's why you want to use AND. An AND will fail (== false) if one or the other (or both) is false. So, if your error count is three or more it will fail. It will also fail if continueLoop is false. If both of them are false, it will fail, too. If you use OR, your check will fail only if both your error count is three or more and continueLoop is false.

John.
S Gregg
Greenhorn

Joined: Oct 13, 2009
Posts: 15
John de Michele wrote:'S':

That's why you want to use AND. An AND will fail (== false) if one or the other (or both) is false. So, if your error count is three or more it will fail. It will also fail if continueLoop is false. If both of them are false, it will fail, too. If you use OR, your check will fail only if both your error count is three or more and continueLoop is false.

John.


ok. Point taken but does anyone know why error will not increment when the exception is caught
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4179
    
  21

S Gregg wrote:
John de Michele wrote:'S':

That's why you want to use AND. An AND will fail (== false) if one or the other (or both) is false. So, if your error count is three or more it will fail. It will also fail if continueLoop is false. If both of them are false, it will fail, too. If you use OR, your check will fail only if both your error count is three or more and continueLoop is false.

John.


ok. Point taken but does anyone know why error will not increment when the exception is caught


Why do you think error isn't incrementing with the exception? Nothing in the code you provided tests it.

<edit>
Ok, I see, because when you have while(error<3 || continueLoop), and that condition never returned false to stop the loop. That isn't because error isn't incrementing, it is because continueLoop is true. With the || (OR) operator, the expression is true as long as at least one of the conditions is true. In this case, when error < 3 returns false continueLoop is still true so the loop is allowed to continue. This is why you need to may the comparison using && (AND) because you only want the loop to continue when there are both < 3 errors AND continueLoop is true.


Steve
S Gregg
Greenhorn

Joined: Oct 13, 2009
Posts: 15
Steve Luke wrote:

Why do you think error isn't incrementing with the exception? Nothing in the code you provided tests it.

<edit>
Ok, I see, because when you have while(error<3 || continueLoop), and that condition never returned false to stop the loop. That isn't because error isn't incrementing, it is because continueLoop is true. With the || (OR) operator, the expression is true as long as at least one of the conditions is true. In this case, when error < 3 returns false continueLoop is still true so the loop is allowed to continue. This is why you need to may the comparison using && (AND) because you only want the loop to continue when there are both < 3 errors AND continueLoop is true.


Well that and the fact that I use Netbeans and set up a watch and it never changes from 0. So now I changed to && and I also decided to decrement count so the loop wouldnt quit after 2 correct entries and one incorrect one and im having another issue. I enter 3 for the number of values then I enter 2, 3 and i and the loop quits.

S Gregg
Greenhorn

Joined: Oct 13, 2009
Posts: 15
Well thank you guys very much for your help but I think I figured it out, I had a couple things that needed to be fixed, using AND instead of OR was one of them
PrasannaKumar Sathiyanantham
Ranch Hand

Joined: Nov 12, 2009
Posts: 110
thats great.....

Be sure to design the program first and then code it....(it saves a lot of time)


To err is human,
To forgive is not company policy
Somnath Mallick
Ranch Hand

Joined: Mar 04, 2009
Posts: 477
Would like to ask that you want to give the user 2 tries per entry or two tries on a whole. If you are giving two attempts per entry then the value of error should be initialized to 0 every time the user enters a correct value.
S Gregg
Greenhorn

Joined: Oct 13, 2009
Posts: 15
PrasannaKumar Sathiyanantham wrote:thats great.....

Be sure to design the program first and then code it....(it saves a lot of time)


I did design the program first but it didn't work out the way had hoped it would so I had to go back to the drawing board and time was running out



Somnath Mallick wrote:Would like to ask that you want to give the user 2 tries per entry or two tries on a whole. If you are giving two attempts per entry then the value of error should be initialized to 0 every time the user enters a correct value.


I realized this error a few hours befre I turned the assignment in and corrected it but thank you.

Here was what I ended up with.

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Error incrementing value in catch block