1

I'm trying to write code which will validate the withdrawal amount. For instance, if the withdrawal amount is greater than the balance, an error should appear and say, "invalid withdrawal amount" but the problem is when I enter a invalid amount, an error message pops up. I try to enter a valid amount and it won't accept anything! It won't even let me close out the dialog box. How can I resolve this?

if (transactionChar== 'W' || transactionChar == 'w')
    {
      input = JOptionPane.showInputDialog("Please enter withdrawal amount."); 
      transactionAmount = Double.parseDouble(input); 
     while(transactionAmount >= oldBalance)
      input = JOptionPane.showInputDialog("Invalid withdrawal amount. Please enter valid withdrawal amount!");

  transactionAmount = Double.parseDouble(input); 

  newBalance = oldBalance - transactionAmount;

} 

My declarations are

double oldBalance= 0;            //Holds the value of the old balance
String transactionType= "";      //Holds the value of the transaction type.
char transactionChar ;           //Gets input for either withdrawal or deposit
double transactionAmount= 0; 

3 Answers 3

2
 while(transactionAmount >= oldBalance)
      input = JOptionPane.showInputDialog("Invalid withdrawal amount. Please enter valid withdrawal amount!");

you never update transactionAmount based on the input, so the while loop never stops

try this instead

while(transactionAmount >= oldBalance) {
      input = JOptionPane.showInputDialog("Invalid withdrawal amount. Please enter valid withdrawal amount!");
      transactionAmount = Double.parseDouble(input);
}

A couple other things you should notice:

If I have $100.00 in my account (oldBalance = 100.0) and I try to withdraw 100.00 from my account (transactionAmount == oldBalance) your code doesn't let me. You don't let me zero out my account!

Also, it appears are going to you let me withdraw a negative amount. That's my kind of withdrawl!! :D

Sign up to request clarification or add additional context in comments.

Comments

0

try using

while(condition) 
{
   statement1;
   statement2;
}

instead of

while(condition)
statement1;
statement2;

Comments

0

It is difficult to answer if you don't format the code correctly, but I think your code is like this:

if (transactionChar== 'W' || transactionChar == 'w') { 
    input = JOptionPane.showInputDialog("Please enter withdrawal amount.");
    transactionAmount = Double.parseDouble(input);
    while(transactionAmount >= oldBalance) 
        input = JOptionPane.showInputDialog("Invalid withdrawal amount. Please enter valid withdrawal amount!");

    transactionAmount = Double.parseDouble(input);
    newBalance = oldBalance - transactionAmount;
}

The problem is that you need a block for your while {} to include the transaction amount assignment:

if (transactionChar== 'W' || transactionChar == 'w') { 
    input = JOptionPane.showInputDialog("Please enter withdrawal amount.");
    transactionAmount = Double.parseDouble(input);
    while(transactionAmount >= oldBalance) {
        input = JOptionPane.showInputDialog("Invalid withdrawal amount. Please enter valid withdrawal amount!");

            transactionAmount = Double.parseDouble(input);
    }
    newBalance = oldBalance - transactionAmount;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.