wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes Suppressing proceeding statements after if statement fails Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Suppressing proceeding statements after if statement fails" Watch "Suppressing proceeding statements after if statement fails" New topic
Author

Suppressing proceeding statements after if statement fails

car michael
Greenhorn

Joined: Oct 16, 2010
Posts: 2
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?
John Eipe
Ranch Hand

Joined: May 23, 2008
Posts: 215
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.


www.cs-repository.info
Kiaamaa Liammes
Ranch Hand

Joined: Oct 03, 2009
Posts: 32



try to separate the logic for debit and credit


SCJP ,SCWCD
John Eipe
Ranch Hand

Joined: May 23, 2008
Posts: 215
you could make it more elegant by making it interactive.


Kiaamaa Liammes
Ranch Hand

Joined: Oct 03, 2009
Posts: 32

It would be better if you do the credit function after doing a check for the bal:




car michael
Greenhorn

Joined: Oct 16, 2010
Posts: 2
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:
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Suppressing proceeding statements after if statement fails