When I try to run my program, it lets me enter in the beginning balance, interest rate, and number of months. However, when it hits the for loop, it completely skips over everything inside of it and prints out the amount that was entered for the starting balance. Can someone please help?

// Create decimal formatter for dollar amount
		DecimalFormat dollar = new DecimalFormat("###,###.00");

		// Get beginning balance
		input = JOptionPane.showInputDialog("Please enter starting account balance: ");

		// Create a BankAccount object
		BankAccount account = new BankAccount(input);

		// Check to make sure dollar amount account balance is valid amount

		// Get interest rate
		input = JOptionPane.showInputDialog("Please enter interest rate: ");

		// Get number of months
		input = JOptionPane.showInputDialog("Please enter number of months: ");

		for (int i= 1; i<= numMonths; i++)
		{	// Begin for loop
			input = JOptionPane.showInputDialog("Please enter deposited amount: " + i);
		    account.deposit(deposit);

		    input = JOptionPane.showInputDialog("Please enter withdrawal amount: " + i);
			account.withdraw(withdraw);
        }	// End for loop

		// Display new balance
		JOptionPane.showMessageDialog(null,
					"The current available balance is $ " +
					dollar.format(account.getBalance()));

Recommended Answers

All 2 Replies

What is the value of numMonths? If it is 0, the for loop won't execute.

From what you posted, it looks like you are storing the input received from the JOptionPane in a variable called input, then overwrite it on the next call. As kramerd mentioned, if the value of numMonths (which in your posted code you do not assign) is 0 or less, the loop will not execute.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.