Results 1 to 5 of 5
  1. #1
    barqcider is offline Member
    Join Date
    Oct 2012
    Posts
    18
    Rep Power
    0

    Default having some issues with nested loops. keeps resetting values and wont work properly.

    i know im missing something here and well, i THINK this is what he expects us to do for this program. i'm not sure though, however im running into an issue where the total deposit resets to 1000. earlier on, i had it where if i put -1 to exit, it did but if i typed in 200 for withdraw, it went into an infinite loop.

    any ideas?

    Java Code:
    import java.util.Scanner;
    public class BankAccount {
    
    	/**
    	 * @param args
    	 */
    	public static void main(String[] args) {
    		// TODO Auto-generated method stub
    		Scanner in= new Scanner(System.in); // reads in user input
    		String accountStatus="Active"; //state of activity for account
    		double acctBalance= 1000; //fixed account balance
    		double withdrawAmt = 0; // user input to amount withdrawn
    		double newBalance = acctBalance-withdrawAmt; // for the new balance after subtracting the withdraw from the account balance
    		 do{
    			System.out.print ("enter withdrawl amount. type -1 to exit: ");
    			withdrawAmt= in.nextDouble();
    			newBalance--;
    			newBalance= acctBalance-withdrawAmt; //new account balance after withdraw
    		 if (accountStatus.equals("Active")&&(acctBalance>=withdrawAmt)){/* checking to see if account is active and withdraw ammount
    		 is equal to or less than the current balance */
    			 System.out.println("your new balance is : "+ newBalance); // If true, this shows the new balance in the account
    		 }
    		 else{
    			 System.out.println("insufficient funds"); // prints if the withdraw amount is greater then the account balance.
    		 }
    		 }
    		 while
    			 (newBalance >=0|| withdrawAmt!=-1);
    		 System.out.println("your new balance is : "+ newBalance);
    			 
    			
    	
    		
    		 
    	}
    
    }// end main

  2. #2
    barqcider is offline Member
    Join Date
    Oct 2012
    Posts
    18
    Rep Power
    0

    Default Re: having some issues with nested loops. keeps resetting values and wont work proper

    it keeps either looping back to the original balance of 1000 instead of decreasing each time i withdraw. it also doesnt exit if i type -1 and keeps adding 1 to the balance.
    any pointers?

  3. #3
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,899
    Rep Power
    25

    Default Re: having some issues with nested loops. keeps resetting values and wont work proper

    When using an OR with multiple conditions, if ANY of the sub conditions are true, the whole expression is true.
    If you used an AND, then ALL of the conditions would have to be true for the whole expression to be true.

    Look at the conditions tested in the while().
    If you don't understand my response, don't ignore it, ask a question.

  4. #4
    barqcider is offline Member
    Join Date
    Oct 2012
    Posts
    18
    Rep Power
    0

    Default Re: having some issues with nested loops. keeps resetting values and wont work proper

    i removed the || and simply made it !=-1, but it keeps adding 1 and not deducting.

    i was testing it so i can get to $0 and get the insufficient funds message, but it only restarts to 1000. even if i put -1, it doesnt kick out.

  5. #5
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,026
    Rep Power
    6

    Default Re: having some issues with nested loops. keeps resetting values and wont work proper

    There are several problems. First, why do you have a newBalance variable? You don't need it from what I can tell. Remove the newBalance-- statement and change all the other newBalance variables to acctBalance.

    The other problem is that when you use -1 to signal the end of the session, you process it as a valid amount. It doesn't hit the while condition till the end. So you need to check for -1 as soon as it is entered and take action immediately.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

Similar Threads

  1. n Nested Loops
    By joyful in forum New To Java
    Replies: 6
    Last Post: 10-16-2012, 05:14 AM
  2. Replies: 20
    Last Post: 03-22-2011, 05:02 AM
  3. Nested Loops
    By candygirl198827 in forum New To Java
    Replies: 38
    Last Post: 12-01-2010, 07:03 AM
  4. class that wont work out properly.
    By vampire-elf in forum New To Java
    Replies: 7
    Last Post: 09-07-2010, 02:39 AM
  5. Why wont my object render properly
    By toecutter in forum AWT / Swing
    Replies: 3
    Last Post: 10-22-2009, 02:43 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
  •