Help coderanch get a
new server
by contributing to the fundraiser
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:
  • Lou Hamers
  • Piet Souris
  • Frits Walraven

Error incrementing value in catch block

 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.

 
Ranch Hand
Posts: 276
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
error < 3 && continueLoop
 
S Gregg
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Rancher
Posts: 600
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
'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
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 600
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
'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
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Bartender
Posts: 4179
22
IntelliJ IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
S Gregg
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 110
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thats great.....

Be sure to design the program first and then code it....(it saves a lot of time)
 
Ranch Hand
Posts: 483
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.

 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic