• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Suppressing proceeding statements after if statement fails

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello everyone

I'm very new to programming and I'm having a bit of trouble with this code.



The program compiles just fine, but I want it to suppress the following statements after an if clause has failed (i.e. entering a negative number for the deposit amount), then initiate the scanner again so the user can input another value for the amount. At the moment, after the first if clause fails the line "Invalid amount. Try again." is printed, then proceeds to the next print line statement.

Any suggestions?
 
Ranch Hand
Posts: 215
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Don't you think that the credit and debit functions should come inside the if block?
In your code, you call the function then you check the condition which is meaningless.
 
Ranch Hand
Posts: 32
Eclipse IDE Objective C Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


try to separate the logic for debit and credit
 
John Eipe
Ranch Hand
Posts: 215
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
you could make it more elegant by making it interactive.


 
Kiaamaa Liammes
Ranch Hand
Posts: 32
Eclipse IDE Objective C Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It would be better if you do the credit function after doing a check for the bal:




 
car michael
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Kiaamaa,

I tried editing my code to look like yours but I got some serious errors when I ran the program. It compiled fine, but when I deposited $2000, it said my balance was $175. This is with a starting balance of $2175.

Also, the else statement does not execute when I enter zero or a negative number. I will post up what my AccountTest class looks like at the moment, as well as the Account class which defines the credit and debit methods.

Also, I should mention that nothing so far has done what I wanted it to do. I want to stop the next statement from printing after the user enters zero or a negative amount for debit, and then allow them to enter another number for debit. So basically, after they enter a negative number, the statement "Invalid amount. Try again." will appear and then the user can enter another number that does not execute the if clause.

So far, these suggestions have indeed made my program look more elegant, but the program still executes the next print line statement after a zero or negative number has been entered for the debit function.

EDIT:

Doh, sorry Kiaamaa. I got $175 for the withdrawal function. Got a little mixed up. Your code works just fine.

Account class:


AccountTest:
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic