Results 1 to 2 of 2
Thread: If Else Statement.. Help
- 10-11-2012, 10:06 PM #1
Member
- Join Date
- Oct 2010
- Posts
- 3
- Rep Power
- 0
If Else Statement.. Help
Hi im new to java coming from C++ and VB i'm kinda lost in the way the Java Code needs to be compiled etc.
my problem is at the moment my If Else Statement
the code below is a boolean of Withdraw that if the Balance is Less then the Amount that they wish to Withdraw then it should show
Insufficient funds But it should then move onto the OverDraft and if the Amount is Less then the overdraft Limit then it should - the Overdraft amount and then output the new balance,
the code it self is Working but its taken off the Overdarft and the Balance of the Bank Account. can some one help me fix this if Statement that if one is false move to the overdraft etc.
Java Code:@Override public boolean withdraw(double amount) { boolean result = false; int overdraft = 1000; /* If The Boolean Result is Turn then Out Print this Message */ System.out.println("Withdraw from Checking Account Holder :" + " " + name); System.out.println("Withdraw from Checking Account Number :" + " "+ Account + " is at the Amount of" + " " + "€" + amount); /* If the Amount to be Taken out is Greater then what the Balance is * Set Result to False * And Print out Message */ if(amount > balance) System.out.println("Insufficient funds on Checking Account Number :" + " " + Account); else //Open Else Statement /* * If the Amount is Lower then the Balance then Subtract the Balance from Amount * And Print out a Message Tell the user Their New Balance On Account. */ balance -= amount; System.out.println("New Balance on Checking Account Number " + Account + " " + "Is " + " " + "€" + balance); if(amount > overdraft ) System.out.println("Insufficient funds on Over Draft"); else System.out.println("Will is now Take from Over Draft"); overdraft -= amount; System.out.println("Overdaft is now at" + " " + overdraft); System.out.println(); return result; // Return Result to be Printed Out onto Screen } //End Method Boolean Withdraw
- 10-12-2012, 01:30 AM #2
Member
- Join Date
- Oct 2012
- Location
- Tempe, Arizona
- Posts
- 77
- Blog Entries
- 12
- Rep Power
- 0
Re: If Else Statement.. Help
If you don't use brackets with an 'if else' statement, the only thing that it reads if the first line after it; unless there are consecutive nested 'if' statements.
Instead use:Java Code:if(condition) System.out.println("hi"); //this will execute with the if statements condition System.out.println("hello") //this will print regardless, since it is not seen as a part of the if statement if(condition) if(condition) //this will execute based on the first if statement if(condition) //this will execute based on the first and second it statements System.out.println("hi") System.out.println("hello") //this will print regardless, since it is not seen as part of the if statements
Java Code:if(condition){ System.out.println("hi"); //this is part of the if statement System.out.println("hello"); //this is part of the if statement } System.out.println("HELLO") //this is not part of the if statement
Hopefully that helps you.
Looking again, does your question mean you want all this:
to execute if the amount is greater than the overdraft? It should all be in the same 'if' statement then, with no 'else'...Java Code:if(amount > overdraft ) System.out.println("Insufficient funds on Over Draft"); else System.out.println("Will is now Take from Over Draft"); overdraft -= amount; System.out.println("Overdaft is now at" + " " + overdraft);Last edited by penguinCoder; 10-12-2012 at 01:34 AM.
Similar Threads
-
if statement
By Exceedinglife in forum New To JavaReplies: 1Last Post: 04-25-2012, 01:25 AM -
the switch statement and unreachable statement error
By name in forum New To JavaReplies: 2Last Post: 03-26-2012, 04:27 PM -
If Statement in SQL
By Steffi1013 in forum JDBCReplies: 6Last Post: 04-10-2010, 03:19 PM -
need help in sql statement
By chyeeqi in forum JDBCReplies: 6Last Post: 03-28-2010, 07:49 PM -
Statement or Prepared Statement ?
By paty in forum JDBCReplies: 3Last Post: 08-01-2007, 04:45 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks