-1

I need to replace spaces commas $ % typed in the jtextfield on my taxcalculator code. But at the same time I need to fix it because when I try to run it again it does not because the validation adds $ to the field. This a simple form of validation such that if the user enters “$1, 499. 97” it will be converted to “1499.97”.

import javax.swing.*;
import java.awt.*;
import java.text.DecimalFormat;

public class TaxCalculator7F extends JApplet {
    JLabel subTotalJLabel, taxRateJLabel, totalTaxJLabel, totalSaleJLabel;
    JTextField subTotalJTextField, taxRateJTextField, totalTaxJTextField, totalSaleJTextField;
    JButton calculateTaxJButton;

    public void init() {
        setLayout(null);  // allows explicit positioning of GUI components

        Container myContent = getContentPane();

        subTotalJLabel = new JLabel();
        subTotalJLabel.setText("Sub Total: ");
        subTotalJLabel.setBounds(10, 0, 100, 25);
        myContent.add(subTotalJLabel);

        subTotalJTextField = new JTextField();
        subTotalJTextField.setText("0.00");
        subTotalJTextField.setHorizontalAlignment(JTextField.RIGHT);
        subTotalJTextField.setBounds(90, 0, 80, 25);
        myContent.add(subTotalJTextField);

        taxRateJLabel = new JLabel();
        taxRateJLabel.setText("Tax Rate (%): ");
        taxRateJLabel.setBounds(10, 40, 100, 25);
        myContent.add(taxRateJLabel);

        taxRateJTextField = new JTextField();
        taxRateJTextField.setText("7");
        taxRateJTextField.setHorizontalAlignment(JTextField.RIGHT);
        taxRateJTextField.setBounds(90, 40, 80, 25);
        myContent.add(taxRateJTextField);

        totalTaxJLabel = new JLabel();
        totalTaxJLabel.setText("Tax Due: ");
        totalTaxJLabel.setBounds(10, 80, 100, 25);
        myContent.add(totalTaxJLabel);

        totalTaxJTextField = new JTextField();
        totalTaxJTextField.setEditable(false);
        totalTaxJTextField.setHorizontalAlignment(JTextField.RIGHT);
        totalTaxJTextField.setBounds(90, 80, 80, 25);
        myContent.add(totalTaxJTextField);

        totalSaleJLabel = new JLabel();
        totalSaleJLabel.setText("Total Sale: ");
        totalSaleJLabel.setBounds(10, 120, 100, 25);
        myContent.add(totalSaleJLabel);

        totalSaleJTextField = new JTextField();
        totalSaleJTextField.setEditable(false);
        totalSaleJTextField.setHorizontalAlignment(JTextField.RIGHT);
        totalSaleJTextField.setBounds(90, 120, 80, 25);
        myContent.add(totalSaleJTextField);

        calculateTaxJButton = new JButton();
        calculateTaxJButton.setText("Calculate Tax");
        calculateTaxJButton.setBounds(20, 160, 140, 30);
        myContent.add(calculateTaxJButton);


        calculateTaxJButton.addActionListener(
                    event -> {
                        // define variables
                        double subTotal, taxRate, taxDue, totalSale;

                        // get sales sub total and tax rate
                        subTotal = Double.parseDouble(subTotalJTextField.getText());
                        //subTotalJTextField.getText();
                        //Code to clear from field $ % ,
                        //subTotalJTextField.setText(String.valueOf(subTotal).replaceAll("(?<=\\d),(?=\\d)|\\$", ""));
                        taxRate = Double.parseDouble(taxRateJTextField.getText());

                        taxDue = subTotal * taxRate / 100;
                        totalSale = subTotal + taxDue;

                        DecimalFormat dollars = new DecimalFormat("$0.00");
                        DecimalFormat percent = new DecimalFormat("0.0%");

                        // test strings
                        subTotalJTextField.setText(dollars.format(subTotal));

                        taxRateJTextField.setText(percent.format(taxRate / 100.00));

                        totalTaxJTextField.setText(dollars.format(taxDue));
                        totalSaleJTextField.setText(dollars.format(totalSale));
                    }
        );
    }
}
Najuto
  • 33
  • 5
  • Welcome to Stack Overflow! I would imagine that 95% of this code is not relevant to your question. Please create a [**Minimal**, Complete and Verifiable Example](http://stackoverflow.com/help/mcve) that demonstrates your issue. – Joe C Jun 30 '17 at 06:07
  • How does it crash? please show your stacktrace – Scary Wombat Jun 30 '17 at 06:07
  • Exception in thread "AWT-EventQueue-1" java.lang.NumberFormatException: For input string: "$100.00" at sun.misc.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:2043) at sun.misc.FloatingDecimal.parseDouble(FloatingDecimal.java:110) at java.lang.Double.parseDouble(Double.java:538) at TaxCalculator7F.lambda$init$0(TaxCalculator7F.java:72) at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2022) .... – Najuto Jun 30 '17 at 06:13

3 Answers3

1

well you could replace the $ sign

subTotal = Double.parseDouble(subTotalJTextField.getText().replace ("$", ""));

and the tax rate would be

taxRate = Double.parseDouble(taxRateJTextField.getText().replace ("%", ""));

or as @nandha points out

    String money = "$1,234,000.67";
    try {
        NumberFormat format = NumberFormat.getCurrencyInstance();
        Number number = format.parse(money);
        System.out.println(number.doubleValue());
    } catch (ParseException e) {
        e.printStackTrace();
    }
Scary Wombat
  • 44,617
  • 6
  • 35
  • 64
  • Tried this, still does not let me run it again. Only runs on the first try. – Najuto Jun 30 '17 at 06:24
  • I guess you are going to need to do it for `taxRate = Double.parseDouble(taxRateJTextField.getText());` as well. Is there some new error coming up? – Scary Wombat Jun 30 '17 at 06:26
  • Ok, I did something now to check, the problem is the % on the tax Rate. Thanks, how can I add to that code on .replace ("$ ", "") to replace commas and spaces as well? – Najuto Jun 30 '17 at 06:27
  • I got it working at the second try with this thanks. But I need to add to it to also replace commas and spaces to " " – Najuto Jun 30 '17 at 06:30
  • Ok. I have tried the answer you gave me . I added it to the code but to the replace. How would it be to include replace , $ spaces % – Najuto Jun 30 '17 at 06:58
  • You can chain them so `taxRateJTextField.getText().replace ("$", "").replace (",", "").replace (" ", "").replace ("%", "")` – Scary Wombat Jun 30 '17 at 07:00
  • Perfect! Now it works ! Sorry if I am a noob, I am beggining to code comming from visual studio and I still need to learn more. Thank you. – Najuto Jun 30 '17 at 07:02
  • No problem - have fun – Scary Wombat Jun 30 '17 at 07:03
1
    String subValue = subTotalJTextField.getText();
    try {
        NumberFormat format = NumberFormat.getCurrencyInstance();
        Number number = format.parse(subValue);
        subTotal = number.doubleValue();
    } catch (ParseException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        subTotal = 0;
    }

Try this. Refer this for more info

Nandha
  • 752
  • 1
  • 12
  • 37
1

I think below code suits well to your problem

Number amount = null, rate = null;
try {
    amount = NumberFormat.getCurrencyInstance().parse(subTotalJTextField.getText());
    rate = NumberFormat.getNumberInstance().parse(taxRateJTextField.getText());
} catch (Exception e) {
    e.printStackTrace();
    amount = 0.0;
    rate = 0.0;
}
subTotal = Double.parseDouble(String.valueOf(amount));
taxRate = Double.parseDouble(String.valueOf(rate));
Saikrishna
  • 51
  • 7