如何生成此错误

时间:2017-06-06 10:08:42

标签: java user-interface netbeans

请协助我使用" $"生成错误。如果我输入负数,我能够成功生成错误但是我无法生成错误,因为我有一个" $"登录我的提款/存款输入。希望这不是一个愚蠢的问题。

每当我想我找到答案时,我会输入代码并弹出一个新问题。

请原谅我的格式。它很可怕,我无法纠正它。学习过程。

private void btnDepositActionPerformed(java.awt.event.ActionEvent evt) {                                           
    int index = jComboBox1.getSelectedIndex();
    DateFormat df = new SimpleDateFormat("yyyy/MM/dd");
    Date date = null;
    try {
        date = df.parse(txtOpenDate.getText());
    } 
        catch (ParseException ex) 
    {
Logger.getLogger(AccountApp.class.getName()).log(Level.SEVERE, null, ex);
    }
    GregorianCalendar cal = new GregorianCalendar();
    cal.setTime(date);
    accounts = new; 
  CheckingAccount(jComboBox1.getSelectedItem().toString(),txtCustomerName.getText(),cal,au.getBalance(index));
String input = (String)JOptionPane.showInputDialog(this,"Deposit amount for account "+jComboBox1.getSelectedItem().toString(),"Deposit to "+jComboBox1.getSelectedItem().toString(),JOptionPane.PLAIN_MESSAGE,null,null,"");
try{
        double amount = Double.parseDouble(input);
        if (amount>=0){
            double balance = accounts.deposit(amount);
            NumberFormat defaultFormat = NumberFormat.getCurrencyInstance();
            txtBalance.setText(defaultFormat.format(balance));
            au.setBalance(index, balance);


        try { au.updateFile(au.getAccountNumber(index), 
             au.getOpenDate(index),au.getCustomerName(index), 
             au.getBalance(index));
            }                                                           
       catch (IOException ex) {
            }
        }
        else {
            NumberFormat defaultFormat = NumberFormat.getCurrencyInstance();
            JOptionPane.showMessageDialog(
                    this, "Invalid deposit amount 
                   "+defaultFormat.format(amount), 
                    "Invalid deposit", JOptionPane.ERROR_MESSAGE);

        }
    }
    catch(Exception e){
    }    

1 个答案:

答案 0 :(得分:0)

简单地使用字符串类中的方法将有助于:

 String x = "$123.33";
 System.out.println(x.contains("$"));
 System.out.println(x.indexOf("$"));