Results 1 to 2 of 2
  1. #1
    java_kiddy is offline Member
    Join Date
    Oct 2010
    Posts
    3
    Rep Power
    0

    Default 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

  2. #2
    penguinCoder is offline Member
    Join Date
    Oct 2012
    Location
    Tempe, Arizona
    Posts
    77
    Blog Entries
    12
    Rep Power
    0

    Default 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.
    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
    Instead use:
    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:
    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);
    to execute if the amount is greater than the overdraft? It should all be in the same 'if' statement then, with no 'else'...
    Last edited by penguinCoder; 10-12-2012 at 01:34 AM.

Similar Threads

  1. if statement
    By Exceedinglife in forum New To Java
    Replies: 1
    Last Post: 04-25-2012, 01:25 AM
  2. Replies: 2
    Last Post: 03-26-2012, 04:27 PM
  3. If Statement in SQL
    By Steffi1013 in forum JDBC
    Replies: 6
    Last Post: 04-10-2010, 03:19 PM
  4. need help in sql statement
    By chyeeqi in forum JDBC
    Replies: 6
    Last Post: 03-28-2010, 07:49 PM
  5. Statement or Prepared Statement ?
    By paty in forum JDBC
    Replies: 3
    Last Post: 08-01-2007, 04:45 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •