-
Double Trouble!!!
Hi, I'm new to Java and am having a problem working with Doubles...I have to write a program that calculates sales tax and a percentage discount. In the red section of the document, near the bottom, when I am writing the firstDouble = DoubleparseDouble lines - see red text below, this is where I am generating there error. I keep getting INT or Possible loss of precision errors - HELP PLEASE - I'd like to understand where I'm going wrong...Thanks :-)
Code:
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.text.*;
public class ProfitMarginCalculator2 extends JApplet implements ActionListener
{
// declarations
Color black = new Color(255, 0, 0);
Color white = new Color(255, 255, 255);
DecimalFormat decimalFormat;
// components
JLabel firstDoubleJLabel;
JTextField firstDoubleJTextField;
JLabel numberofItemsJLabel;
JTextField numberofItemsJTextField;
JLabel PERCENTAGE_DOUBLEJLabel;
JTextField PERCENTAGE_DOUBLEJTextField;
JLabel subTotalJLabel;
JTextField subTotalJTextField;
JLabel amountOfDiscountJLabel;
JTextField amountOfDiscountJTextField;
JLabel salesTaxJLabel;
JTextField salesTaxJTextField;
JLabel discounttotalSalesJLabel;
JTextField discounttotalSalesJTextField;
JButton enterJButton;
JButton clearJButton;
// variables
int numberofItems;
int subTotal;
int amountOfDiscount;
[COLOR="red"]double salesTax;[/COLOR] int discounttotalSales;
[COLOR="red"]double firstDouble;
double PERCENTAGE_DOUBLE = .7;[/COLOR]
public void init ()
{
setLayout(null);
setSize(400, 400);
// initialize components
firstDoubleJLabel = new JLabel();
firstDoubleJLabel.setBounds(50, 10, 150, 20);
firstDoubleJLabel.setFont(new Font("Default", Font.PLAIN, 12));
firstDoubleJLabel.setText("Price of Item");
firstDoubleJLabel.setForeground(black);
firstDoubleJLabel.setHorizontalAlignment(JLabel.LEFT);
add(firstDoubleJLabel);
firstDoubleJTextField = new JTextField();
firstDoubleJTextField.setBounds(200, 10, 50, 20);
firstDoubleJTextField.setFont(new Font("Default", Font.PLAIN, 12));
firstDoubleJTextField.setHorizontalAlignment(JTextField.CENTER);
firstDoubleJTextField.setForeground(black);
firstDoubleJTextField.setBackground(white);
firstDoubleJTextField.setEditable(true);
add(firstDoubleJTextField);
numberofItemsJLabel = new JLabel();
numberofItemsJLabel.setBounds(50, 50, 150, 20);
numberofItemsJLabel.setFont(new Font("Default", Font.PLAIN, 12));
numberofItemsJLabel.setText("Number of Items");
numberofItemsJLabel.setForeground(black);
numberofItemsJLabel.setHorizontalAlignment(JLabel.LEFT);
add(numberofItemsJLabel);
numberofItemsJTextField = new JTextField();
numberofItemsJTextField.setBounds(200, 50, 50, 20);
numberofItemsJTextField.setFont(new Font("Default", Font.PLAIN, 12));
numberofItemsJTextField.setHorizontalAlignment(JTextField.CENTER);
numberofItemsJTextField.setForeground(black);
numberofItemsJTextField.setBackground(white);
numberofItemsJTextField.setEditable(true);
add(numberofItemsJTextField);
PERCENTAGE_DOUBLEJLabel = new JLabel();
PERCENTAGE_DOUBLEJLabel.setBounds(50, 100, 150, 20);
PERCENTAGE_DOUBLEJLabel.setFont(new Font("Default", Font.PLAIN, 12));
PERCENTAGE_DOUBLEJLabel.setText("Discount Percentage");
PERCENTAGE_DOUBLEJLabel.setForeground(black);
PERCENTAGE_DOUBLEJLabel.setHorizontalAlignment(JLabel.LEFT);
add(PERCENTAGE_DOUBLEJLabel);
PERCENTAGE_DOUBLEJTextField = new JTextField();
PERCENTAGE_DOUBLEJTextField.setBounds(200, 100, 50, 20);
PERCENTAGE_DOUBLEJTextField.setFont(new Font("Default", Font.PLAIN, 12));
PERCENTAGE_DOUBLEJTextField.setHorizontalAlignment(JTextField.CENTER);
PERCENTAGE_DOUBLEJTextField.setForeground(black);
PERCENTAGE_DOUBLEJTextField.setBackground(white);
PERCENTAGE_DOUBLEJTextField.setEditable(true);
add(PERCENTAGE_DOUBLEJTextField);
subTotalJLabel = new JLabel();
subTotalJLabel.setBounds(50, 150, 150, 20);
subTotalJLabel.setFont(new Font("Default", Font.PLAIN, 12));
subTotalJLabel.setText("Sub Total");
subTotalJLabel.setForeground(black);
subTotalJLabel.setHorizontalAlignment(JLabel.LEFT);
add(subTotalJLabel);
subTotalJTextField = new JTextField();
decimalFormat = new DecimalFormat("$0.00");
subTotalJTextField.setBounds(200, 150, 50, 20);
subTotalJTextField.setFont(new Font("Default", Font.PLAIN, 12));
subTotalJTextField.setHorizontalAlignment(JTextField.CENTER);
subTotalJTextField.setForeground(black);
subTotalJTextField.setBackground(white);
subTotalJTextField.setEditable(true);
add(subTotalJTextField);
amountOfDiscountJLabel = new JLabel();
amountOfDiscountJLabel.setBounds(50, 200, 150, 20);
amountOfDiscountJLabel.setFont(new Font("Default", Font.PLAIN, 12));
amountOfDiscountJLabel.setText("Amount of Discount");
amountOfDiscountJLabel.setForeground(black);
amountOfDiscountJLabel.setHorizontalAlignment(JLabel.LEFT);
add(amountOfDiscountJLabel);
amountOfDiscountJTextField = new JTextField();
decimalFormat = new DecimalFormat("$0.00");
amountOfDiscountJTextField.setBounds(200, 200, 50, 20);
amountOfDiscountJTextField.setFont(new Font("Default", Font.PLAIN, 12));
amountOfDiscountJTextField.setHorizontalAlignment(JTextField.CENTER);
amountOfDiscountJTextField.setForeground(black);
amountOfDiscountJTextField.setBackground(white);
amountOfDiscountJTextField.setEditable(true);
add(amountOfDiscountJTextField);
salesTaxJLabel = new JLabel();
salesTaxJLabel.setBounds(50, 250, 150, 20);
salesTaxJLabel.setFont(new Font("Default", Font.PLAIN, 12));
salesTaxJLabel.setText("Sales Tax");
salesTaxJLabel.setForeground(black);
salesTaxJLabel.setHorizontalAlignment(JLabel.LEFT);
add(salesTaxJLabel);
salesTaxJTextField = new JTextField();
decimalFormat = new DecimalFormat("$0.00");
salesTaxJTextField.setBounds(200, 250, 50, 20);
salesTaxJTextField.setFont(new Font("Default", Font.PLAIN, 12));
salesTaxJTextField.setHorizontalAlignment(JTextField.CENTER);
salesTaxJTextField.setForeground(black);
salesTaxJTextField.setBackground(white);
salesTaxJTextField.setEditable(true);
add(salesTaxJTextField);
discounttotalSalesJLabel = new JLabel();
discounttotalSalesJLabel.setBounds(50, 300, 150, 20);
discounttotalSalesJLabel.setFont(new Font("Default", Font.PLAIN, 12));
discounttotalSalesJLabel.setText("Discount Total Sales");
discounttotalSalesJLabel.setForeground(black);
discounttotalSalesJLabel.setHorizontalAlignment(JLabel.LEFT);
add(discounttotalSalesJLabel);
discounttotalSalesJTextField = new JTextField();
decimalFormat = new DecimalFormat("$0.00");
discounttotalSalesJTextField.setBounds(200, 300, 50, 20);
discounttotalSalesJTextField.setFont(new Font("Default", Font.PLAIN, 12));
discounttotalSalesJTextField.setHorizontalAlignment(JTextField.CENTER);
discounttotalSalesJTextField.setForeground(black);
discounttotalSalesJTextField.setBackground(white);
discounttotalSalesJTextField.setEditable(true);
add(discounttotalSalesJTextField);
enterJButton = new JButton();
enterJButton.setBounds(50, 350, 100, 20);
enterJButton.setFont(new Font("Default", Font.PLAIN, 12));
enterJButton.setText("Enter");
enterJButton.setForeground(black);
enterJButton.setBackground(white);
add(enterJButton);
enterJButton.addActionListener(this);
clearJButton = new JButton();
clearJButton.setBounds(190, 350, 80, 20);
clearJButton.setFont(new Font("Default", Font.PLAIN, 12));
clearJButton.setText("Clear");
clearJButton.setForeground(black);
clearJButton.setBackground(white);
add(clearJButton);
clearJButton.addActionListener(this);
}
public void actionPerformed(ActionEvent event)
{
Object obj = event.getSource();
if(obj == enterJButton)
{
getfirstDouble();
}
else if(obj == clearJButton)
{
clearAll();
}
}
public void getfirstDouble()
{
firstDouble = Double.parseDouble(firstDoubleJTextField.getText());
getnumberofItems();
}
public void getnumberofItems()
{
numberofItems = Integer.parseInt(numberofItemsJTextField.getText());
getPERCENTAGE_DOUBLE();
}
[COLOR="red"] public void getPERCENTAGE_DOUBLE()
{
PERCENTAGE_DOUBLE = Double.parseDouble(PERCENTAGE_DOUBLEJTextField.getText());
PERCENTAGE_DOUBLE = firstDouble * PERCENTAGE_DOUBLE;
getsubTotal();
}[/COLOR]
public void getsubTotal()
{
subTotal = Integer.parseInt(subTotalJTextField.getText());
subTotal = firstDouble * numberofItems;
displaysubTotal();
getamountOfDiscount();
}
[COLOR="red"] public void getamountOfDiscount()
{
amountOfDiscount = Integer.parseInt(amountOfDiscountJTextField.getText());
amountOfDiscount = subTotal - (subTotal * PERCENTAGE_DOUBLE);
amountOfDiscountJTextField.setText("" + decimalFormat.format(amountOfDiscount));
getsalesTax();
}[/COLOR]
[COLOR="Red"] public void getsalesTax()
{
salesTax = Double.parseDouble(salesTaxJTextField.getText());
salesTax = amountOfDiscount * .7;
displaysalesTax();
getdiscounttotalSales();
}[/COLOR]
public void getdiscounttotalSales()
{
discounttotalSales = (amountOfDiscount + salesTax);
displaydiscounttotalSales();
}
public void displaynumberofItems()
{
numberofItemsJTextField.setText("" + numberofItems);
}
public void displaysubTotal()
{
subTotalJTextField.setText("" + decimalFormat.format(subTotal));
}
public void displayamountOfDiscount()
{
amountOfDiscountJTextField.setText("" + decimalFormat.format(amountOfDiscount));
}
public void displaysalesTax()
{
salesTaxJTextField.setText("" + decimalFormat.format(salesTax));
}
public void displaydiscounttotalSales()
{
discounttotalSalesJTextField.setText("" + decimalFormat.format(discounttotalSales));
}
public void clearAll()
{
discounttotalSalesJTextField.setText("");
discounttotalSalesJTextField.requestFocusInWindow();
salesTaxJTextField.setText("");
salesTaxJTextField.requestFocusInWindow();
amountOfDiscountJTextField.setText("");
amountOfDiscountJTextField.requestFocusInWindow();
subTotalJTextField.setText("");
subTotalJTextField.requestFocusInWindow();
PERCENTAGE_DOUBLEJTextField.setText("");
PERCENTAGE_DOUBLEJTextField.requestFocusInWindow();
numberofItemsJTextField.setText("");
numberofItemsJTextField.requestFocusInWindow();
firstDoubleJTextField.setText("");
firstDoubleJTextField.requestFocusInWindow();
}
}
-
To increase your chances of getting help, I recommend that you use code tags to make your code readable, and try to simplify the problem (and the code) for us as much as possible. What lines are causing the problem? What is specifically being output that shouldn't be?
Welcome and much luck!
-
thanks - i at least now know what a code tag is...adjustments made!
-
I've looked at your code a little. Perhaps you're using int variables where a double should be used since it will be holding double information. Think on this and it will come to you.
<soapbox>
Also, out of respect towards Fishtoprecords and other experts here, I'd be remiss if I didn't state that in the real world financial information should never be represented as a double as this data should not be subjected to any significant rounding error.
</soapbox>
Having said that, you are working under different constraints: the constraints of your instructors, and so my practical advice is to use doubles if they tell you to.
-
i changed my variables to doubles and then my Integer.parseInt lines to Double.parseDouble - it ran successfully! Now to figure everything else out - THANKS - Nicole