Skip to Main Content

New to Java

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Catch Negative Number

843789Oct 5 2009 — edited Oct 5 2009
Hi experts,

Is anyone can teach me how to catch negative input in GUI. I use NumberFormatException but it only catches String. I'd like to know in either way (try-catch and throwing exception.



private void btnEnterActionPerformed(java.awt.event.ActionEvent evt)
{
String amount = txtAmount.getText();
if(!amount.equals(null) && !amount.equals(""))
{
try
{
double balance = depositTransaction.deposit(Double.parseDouble(this.txtAmount.getText()));
lblBalance.setText("Your account balance is: $" + balance);
}
catch(NumberFormatException e)
{
lblBalance.setText("Invalid deposit entry");
}
txtAmount.setText("");
btnEnter.setEnabled(false);
}
}

Thanks heaps.
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Nov 2 2009
Added on Oct 5 2009
4 comments
3,040 views