0

Here is my code for my program.

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

public class SalesTax extends JFrame
{
    private JPanel panel;               // A panel to hold everything
    private JTextField totalSales;      // To get total sales
    private JButton calcButton;         // Calculates everything

    // Constants for tax rates
    private final double COUNTY_RATE = 0.02;
    private final double STATE_RATE = 0.04;

    // Contants for window size
    private final int WINDOW_WIDTH = 360;
    private final int WINDOW_HEIGHT = 100;

    /**
       Constructor
     */

    public SalesTax()
    {
        //Set the title
        setTitle("Monthly Sales Tax Reporter");

        // Specify what happens when the close button is clicked
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        // Build the panel that contains the other components
        buildPanel();

        // Add the panel to the content pane
        add(panel);

        // Size and display the window
        setSize(WINDOW_WIDTH, WINDOW_HEIGHT);
        setVisible(true);
    }

    /**
       The buildPanel method creates a panel containing
       other components
     */

    private void buildPanel()
    {
        // Create a label promoting for the total sales
        JLabel totalSalesMsg =
                    new JLabel("Enter the total sales: ");

       // Create a text field for total sales
       totalSales = new JTextField(10);

       // Create a button to click
       calcButton = new JButton("Calculate Sales Tax");

       // Add an action listener to the button
       calcButton.addActionListener(new CalcButtonListener());

       // Create a panel
       panel = new JPanel();

       // Add the label, text field, and button to the panel
       panel.add(totalSalesMsg);
       panel.add(totalSales);
       panel.add(calcButton);

   }

    /** CalcButtonListener is an action listener class for the
        calcButton component
     */

    private class CalcButtonListener implements ActionListener
    {
        /**
           actionPerformed method
           @paran e An ActionEvent object
        */

        public void actionPerformed(ActionEvent e)
        {
            double totalSalesAmount,
                   countyTaxAmount,
                   stateTaxAmount,
                   totalTaxAmount;

            // Create a DecimalFormat object to format output
            DecimalFormat dollar = new DecimalFormat("#.##0.00");

            // Get the total sales
            totalSalesAmount = Double.parseDouble(totalSales.getText());

            // Calculate the county tax
            countyTaxAmount = totalSalesAmount * COUNTY_RATE;

            // Calculate the state tax
            stateTaxAmount = totalSalesAmount * STATE_RATE;

            // Calculate the total sales
            totalTaxAmount = countyTaxAmount + stateTaxAmount;

            // Display the results
            JOptionPane.showMessageDialog(null, "County Sales Tax: $" +
                                    dollar.format(countyTaxAmount) +
                                    "\nState Sales Tax: $" +
                                    dollar.format(stateTaxAmount) +
                                    "\nTotal Sales Tax: $" +
                                    dollar.format(totalTaxAmount));


        }        
    }

    public static void main(String[] args)
    {
        SalesTax stw = new SalesTax();
    }        

}

Here is what I get.

run:
Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException: Multiple decimal separators in pattern "#.##0.00"
    at java.text.DecimalFormat.applyPattern(DecimalFormat.java:2519)
    at java.text.DecimalFormat.<init>(DecimalFormat.java:416)
    at SalesTax$CalcButtonListener.actionPerformed(SalesTax.java:91)
    at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1995)
    at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2318)
    at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:387)
    at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:242)
    at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:236)
    at java.awt.Component.processMouseEvent(Component.java:6290)
    at javax.swing.JComponent.processMouseEvent(JComponent.java:3267)
    at java.awt.Component.processEvent(Component.java:6055)
    at java.awt.Container.processEvent(Container.java:2039)
    at java.awt.Component.dispatchEventImpl(Component.java:4653)
    at java.awt.Container.dispatchEventImpl(Container.java:2097)
    at java.awt.Component.dispatchEvent(Component.java:4481)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4575)
    at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4236)
    at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4166)
    at java.awt.Container.dispatchEventImpl(Container.java:2083)
    at java.awt.Window.dispatchEventImpl(Window.java:2482)
    at java.awt.Component.dispatchEvent(Component.java:4481)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:648)
    at java.awt.EventQueue.access$000(EventQueue.java:84)
    at java.awt.EventQueue$1.run(EventQueue.java:607)
    at java.awt.EventQueue$1.run(EventQueue.java:605)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:87)
    at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:98)
    at java.awt.EventQueue$2.run(EventQueue.java:621)
    at java.awt.EventQueue$2.run(EventQueue.java:619)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:87)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:618)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)
Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException: Multiple decimal separators in pattern "#.##0.00"
    at java.text.DecimalFormat.applyPattern(DecimalFormat.java:2519)
    at java.text.DecimalFormat.<init>(DecimalFormat.java:416)
    at SalesTax$CalcButtonListener.actionPerformed(SalesTax.java:91)
    at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1995)
    at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2318)
    at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:387)
    at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:242)
    at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:236)
    at java.awt.Component.processMouseEvent(Component.java:6290)
    at javax.swing.JComponent.processMouseEvent(JComponent.java:3267)
    at java.awt.Component.processEvent(Component.java:6055)
    at java.awt.Container.processEvent(Container.java:2039)
    at java.awt.Component.dispatchEventImpl(Component.java:4653)
    at java.awt.Container.dispatchEventImpl(Container.java:2097)
    at java.awt.Component.dispatchEvent(Component.java:4481)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4575)
    at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4236)
    at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4166)
    at java.awt.Container.dispatchEventImpl(Container.java:2083)
    at java.awt.Window.dispatchEventImpl(Window.java:2482)
    at java.awt.Component.dispatchEvent(Component.java:4481)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:648)
    at java.awt.EventQueue.access$000(EventQueue.java:84)
    at java.awt.EventQueue$1.run(EventQueue.java:607)
    at java.awt.EventQueue$1.run(EventQueue.java:605)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:87)
    at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:98)
    at java.awt.EventQueue$2.run(EventQueue.java:621)
    at java.awt.EventQueue$2.run(EventQueue.java:619)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:87)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:618)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)
BUILD SUCCESSFUL (total time: 2 minutes 22 seconds)

I can't run the entire program, I don't know why.

2
Contributors
1
Reply
16
Views
4 Years
Discussion Span
Last Post by bguild
0

By default DecimalFormat uses nonlocalized patterns, which means you need to use ',' as your digit group separator in the pattern. It is done this way so that you can use your pattern anywhere, but it is also possible to use localized patterns. For example, this works fine for me:

DecimalFormat df = new DecimalFormat();
df.setDecimalFormatSymbols(DecimalFormatSymbols.getInstance(Locale.GERMANY));
df.applyLocalizedPattern("#.##0.00");

I only need line 2 because my default locale uses ',' as the group separator. I chose Germany for the '.' group separator. Since you are trying to use '.' as a group separator, I expect your default locale is already set to something appropriate, so all you need to do is call applyLocalizedPattern instead of giving your pattern directly to the constructor.

This might be useful if you were getting your pattern from the user. Otherwise, all it will accomplish is causing your application to throw an exception in some locales.

Edited by bguild

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.