Results 1 to 5 of 5
  1. #1
    calculuslord is offline Member
    Join Date
    Oct 2009
    Location
    east coast
    Posts
    7
    Rep Power
    0

    Default 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 :-)

    Java 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();
    	}
    
    
    }
    Last edited by calculuslord; 10-18-2009 at 07:31 PM. Reason: trying to take moderator's advice...thanks

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    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!

  3. #3
    calculuslord is offline Member
    Join Date
    Oct 2009
    Location
    east coast
    Posts
    7
    Rep Power
    0

    Default

    thanks - i at least now know what a code tag is...adjustments made!

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    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.

  5. #5
    calculuslord is offline Member
    Join Date
    Oct 2009
    Location
    east coast
    Posts
    7
    Rep Power
    0

    Default

    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

Similar Threads

  1. Check if double is double
    By marshalthrone in forum New To Java
    Replies: 8
    Last Post: 09-30-2009, 03:51 PM
  2. long to double trouble (Rhyme scheme not intended)
    By sdeverteuil in forum New To Java
    Replies: 2
    Last Post: 08-19-2009, 12:02 AM
  3. Replies: 5
    Last Post: 03-21-2009, 10:32 PM
  4. Double
    By Xystus777 in forum New To Java
    Replies: 2
    Last Post: 01-21-2009, 11:39 AM
  5. Double.valueOf() vs Double.parseDouble()
    By greenbean in forum New To Java
    Replies: 10
    Last Post: 01-12-2009, 09:39 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •