Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 19 of 19

Thread: My program is not displaying the answer :(

  1. #1
    Junior Member
    Join Date
    Jul 2014
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default My program is not displaying the answer :(

    Hey guys I'm a little lost at the moment. I'm pretty sure everything is functioning properly, but my answer will not display at all. This is my first Java "multi-class" program... and I have to say I'm a little confused as to its exact purpose. BUT nonetheless here is my following code:

    /*
    	program:AcmeSelfServe
    	name: Jacob Bosiljevac
    	date: July.2014
    */
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import java.text.*;
    import java.text.DateFormat;
    import java.util.Date;
     
    public class AcmeSelfServe extends JFrame
    {
    	// declarations
    	Color black = new Color(0, 0, 0);
    	Color white = new Color(255, 255, 255);
    	Color light_gray = new Color(192, 192, 192);
     
    	DecimalFormat decimalFormat;
    	DateFormat dateFormat;
    	SalesAmount salesAmount;
     
    	JRadioButton regularJRadioButton;
       JLabel regularJLabel;
     
    	JRadioButton premiumJRadioButton;
       JLabel premiumJLabel;
     
       JRadioButton superJRadioButton;
       JLabel superJLabel;
     
    	ButtonGroup displayButtonGroup;
     
    	JLabel numberOfGallonsJLabel;
    	JTextField numberOfGallonsJTextField;
     
    	JLabel totalJLabel;
    	JTextField totalJTextField;
     
     
    	// controls
    	JButton enterJButton;
    	JButton clearJButton;
    	JButton closeJButton;
     
    	// variables & constants
    	float saleAmount;
    	int quantityOfItem;
    	float gasRate;
     
    	double salesSubTotal;
     
    	float totalAmountOfSale;
     
       	public AcmeSelfServe()
       	{
          		createUserInterface();
       	}
     
       	public void createUserInterface()
       	{
    		Container contentPane = getContentPane();
    		contentPane.setBackground(white);
    		contentPane.setLayout(null);
     
    		// inputs
     
    		numberOfGallonsJLabel = new JLabel();
    		numberOfGallonsJLabel.setBounds(50, 50, 150, 20);
    		numberOfGallonsJLabel.setFont(new Font("Default", Font.PLAIN, 12));
    		numberOfGallonsJLabel.setText("Number of Gallons");
    		numberOfGallonsJLabel.setForeground(black);
    		numberOfGallonsJLabel.setHorizontalAlignment(JLabel.RIGHT);
    		contentPane.add(numberOfGallonsJLabel);
     
    		numberOfGallonsJTextField = new JTextField();
    		numberOfGallonsJTextField.setBounds(210, 50, 50, 20);
    		numberOfGallonsJTextField.setFont(new Font("Default", Font.PLAIN, 12));
    		numberOfGallonsJTextField.setHorizontalAlignment(JTextField.CENTER);
    		numberOfGallonsJTextField.setForeground(black);
    		numberOfGallonsJTextField.setBackground(white);
    		numberOfGallonsJTextField.setEditable(true);
    		contentPane.add(numberOfGallonsJTextField);
     
    		displayButtonGroup = new ButtonGroup();
     
    		regularJRadioButton = new JRadioButton();
    		regularJRadioButton.setBounds(100, 80, 150, 20);
    		regularJRadioButton.setText("Regular         $3.55");
    		regularJRadioButton.setSelected(true);
    		regularJRadioButton.setForeground(black);
    		regularJRadioButton.setBackground(white);
    		displayButtonGroup.add(regularJRadioButton);
    		contentPane.add(regularJRadioButton);
     
    		premiumJRadioButton = new JRadioButton();
    		premiumJRadioButton.setBounds(100, 110, 150, 20);
    		premiumJRadioButton.setText("Premium      $4.95");
    		premiumJRadioButton.setForeground(black);
    		premiumJRadioButton.setBackground(white);
    		displayButtonGroup.add(premiumJRadioButton);
    		contentPane.add(premiumJRadioButton);
     
          superJRadioButton = new JRadioButton();
    		superJRadioButton.setBounds(100, 140, 150, 20);
    		superJRadioButton.setText("Super            $5.25");
    		superJRadioButton.setForeground(black);
    		superJRadioButton.setBackground(white);
    		displayButtonGroup.add(superJRadioButton);
    		contentPane.add(superJRadioButton);
     
    		// output
     
    		totalJLabel = new JLabel();
    		totalJLabel.setBounds(50, 200, 100, 20);
    		totalJLabel.setFont(new Font("Default", Font.PLAIN, 12));
    		totalJLabel.setText("Total Sale");
    		totalJLabel.setForeground(black);
    		totalJLabel.setHorizontalAlignment(JLabel.RIGHT);
    		contentPane.add(totalJLabel);
     
    		totalJTextField = new JTextField();
    		totalJTextField.setBounds(210, 200, 50, 20);
    		totalJTextField.setFont(new Font("Default", Font.PLAIN, 12));
    		totalJTextField.setHorizontalAlignment(JTextField.CENTER);
    		totalJTextField.setForeground(black);
    		totalJTextField.setBackground(white);
    		totalJTextField.setEditable(false);
    		contentPane.add(totalJTextField);
     
     
    		// control
    		enterJButton = new JButton();
    		enterJButton.setBounds(50, 300, 80, 20);
    		enterJButton.setFont(new Font("Default", Font.PLAIN, 12));
    		enterJButton.setText("Enter");
    		enterJButton.setForeground(black);
    		enterJButton.setBackground(white);
    		contentPane.add(enterJButton);
    		enterJButton.addActionListener(
     
    			new ActionListener()
    			{
    				public void actionPerformed(ActionEvent event)
    				{
    					enterJButtonActionPerformed(event);
    				}
    			}
    		);
     
    		clearJButton = new JButton();
    		clearJButton.setBounds(140, 300, 80, 20);
    		clearJButton.setFont(new Font("Default", Font.PLAIN, 12));
    		clearJButton.setText("Clear");
    		clearJButton.setForeground(black);
    		clearJButton.setBackground(white);
    		contentPane.add(clearJButton);
    		clearJButton.addActionListener(
     
    			new ActionListener()
    			{
    				public void actionPerformed(ActionEvent event)
    				{
    					clearJButtonActionPerformed(event);
    				}
    			}
    		);
     
    		closeJButton = new JButton();
    		closeJButton.setBounds(230, 300, 80, 20);
    		closeJButton.setFont(new Font("Default", Font.PLAIN, 12));
    		closeJButton.setText("Close");
    		closeJButton.setForeground(black);
    		closeJButton.setBackground(white);
    		contentPane.add(closeJButton);
    		closeJButton.addActionListener(
     
    			new ActionListener()
    			{
    				public void actionPerformed(ActionEvent event)
    				{
    					closeJButtonActionPerformed(event);
    				}
    			}
    		);
     
    		setTitle("Acme Self Serve");
    		setSize( 400, 400 );
    		setVisible(true);
       	}
     
       	// main method
       	public static void main(String[] args)
       	{
          		AcmeSelfServe application = new AcmeSelfServe();
          		application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
       	}
     
       	public void enterJButtonActionPerformed(ActionEvent event)
          {
     
    		try
    		{
    			quantityOfItem = Integer.parseInt(numberOfGallonsJTextField.getText());
    			getGasRate();
    		}
    		catch(NumberFormatException exception)
    		 {
    		      	JOptionPane.showMessageDialog(this,
    		       	"Please enter number of gallons!",
    		        "Number Format Error", JOptionPane.ERROR_MESSAGE );
    		        numberOfGallonsJTextField.setText("");
    		        numberOfGallonsJTextField.requestFocusInWindow();
    		}
    	}
     
    	public void getGasRate()
    	{
    		if(regularJRadioButton.isSelected())
    		{
    			double gasRate = 3.55;
    		}
     
    		if(premiumJRadioButton.isSelected())
    		{
    			double gasRate = 4.95;
    		}
          if(superJRadioButton.isSelected())
          {
             double gasRate = 5.25;
          }
    		calculateSubTotal();
    	}
     
    	public void calculateSubTotal()
    	{
    		salesAmount = new SalesAmount(saleAmount, quantityOfItem);
    		salesSubTotal = salesAmount.getSubTotal();
    	}
     
    	public void displaySales()
    	{
    		decimalFormat = new DecimalFormat("$0.00");
          totalJTextField.setText("" + decimalFormat.format(saleAmount));
    		totalJTextField.setText("" + salesSubTotal);
    	}
     
    	public void clearJButtonActionPerformed(ActionEvent event)
    	{
    		numberOfGallonsJTextField.setText("");
          numberOfGallonsJTextField.requestFocusInWindow();
    		totalJTextField.setText("");
    		regularJRadioButton.setSelected(true);
    	}
     
    	public void closeJButtonActionPerformed(ActionEvent event)
    	{
    		AcmeSelfServe.this.dispose();
    	}
    }
     
    class SalesAmount
    {
    	int quantityOfItems;
    	float salesSubTotal;
       float gasRate;
     
     
     
    	public SalesAmount(float saleValue, int quantityValue)
    	{
    		gasRate = saleValue;
    		quantityOfItems = quantityValue;
    		salesSubTotal = gasRate * quantityOfItems;
    	}
     
    	public float getSubTotal()
    	{
     
    		return salesSubTotal;
    	}
    }

    Any help would be appreciated!


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: My program is not displaying the answer :(

    my answer will not display at all.
    What are you expecting to be displayed?
    Where are you expecting it to be displayed?
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Jul 2014
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: My program is not displaying the answer :(

    I want the salesSubTotal to be displayed in the totalJTextField.. :/

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: My program is not displaying the answer :(

    I see some code to set totalJTextField's value.
    Is the code that does that being executed?

    Trace back through the methods that are called to see if the code to set the value is being executed.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Jul 2014
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: My program is not displaying the answer :(

    I can't tell

    My window pops up with me being able to select everything, but the textfield does not display the answer. I keep scanning through with no success!

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: My program is not displaying the answer :(

    Do it one step at a time:
    Where is totalJTextField set to a value? What method is the statement in?
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    Jul 2014
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: My program is not displaying the answer :(

    So I added

    Public void calculateSubTotal()
    	{
    		salesAmount = new SalesAmount(saleAmount, quantityOfItem);
    		salesSubTotal = salesAmount.getSubTotal();
          displaySales(); // <---- New
    	}

    Now it displays 0.0 in my JTextField >.< I guess that's a step in the right direction. I have to figure out how to make sure my equation is being sent back properly.

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: My program is not displaying the answer :(

    Ok, that's progress. Now you see a value. Now Where is the variable whose value is placed in the textfield, assigned a value?
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Junior Member
    Join Date
    Jul 2014
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: My program is not displaying the answer :(

    I think it's in my class SalesAmount, float salesSubTotal.

    I returned it at the end though, I can't see where the disconnect is >_<

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: My program is not displaying the answer :(

    Where is the value computed that is assigned to the variable shown in the textfield?
    Look at the contents of the variables used in the computation to see if they are correct.
    For example are they all non-zero values?
    If you don't understand my answer, don't ignore it, ask a question.

  11. #11
    Junior Member
    Join Date
    Jul 2014
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: My program is not displaying the answer :(

    I think I'm gonna take a 15 minute break and come back to it. Maybe I'll be able to spot it when I come back xD

    --- Update ---

    Are you saying that I might have a zero value that's making my answer be 0.0?

  12. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: My program is not displaying the answer :(

    The way to tell is to debug the code. I would add several println() statements to print out the values of all the variables used in the computations to see what their values are.
    If you don't understand my answer, don't ignore it, ask a question.

  13. #13
    Member
    Join Date
    Apr 2014
    Posts
    219
    Thanks
    8
    Thanked 2 Times in 2 Posts

    Default Re: My program is not displaying the answer :(

    *** code removed

    You were not putting displaySales() in your try statement, you were not setting your global variables once your radio buttons were selected, you were just creating another variable. Conflicting variables too with floats and double, pick one and stick with it. I tinkered with it for a few minutes and got it working, take a look....

    public void calculateSubTotal()
    {
    	salesAmount = new SalesAmount(gasRate, quantityOfItem); // you were sending in salesAmount here which had no value
    	salesSubTotal = salesAmount.getSubTotal();
    }

    The program need more work but this should get you there.... Your welcome.
    Last edited by Norm; July 14th, 2014 at 01:57 PM. Reason: Please don't do OPs work for him

  14. #14
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: My program is not displaying the answer :(

    If you don't understand my answer, don't ignore it, ask a question.

  15. #15
    Member
    Join Date
    Apr 2014
    Posts
    219
    Thanks
    8
    Thanked 2 Times in 2 Posts

    Default Re: My program is not displaying the answer :(

    sorry.....

  16. #16
    Junior Member
    Join Date
    Jul 2014
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: My program is not displaying the answer :(

    Quote Originally Posted by jocdrew21 View Post
    *** code removed

    You were not putting displaySales() in your try statement, you were not setting your global variables once your radio buttons were selected, you were just creating another variable. Conflicting variables too with floats and double, pick one and stick with it. I tinkered with it for a few minutes and got it working, take a look....

    public void calculateSubTotal()
    {
    	salesAmount = new SalesAmount(gasRate, quantityOfItem); // you were sending in salesAmount here which had no value
    	salesSubTotal = salesAmount.getSubTotal();
    }

    The program need more work but this should get you there.... Your welcome.
    How do I set global values? What's the difference between what I did, and making them "Global."

    salesAmount has no value but didn't I give it a value by assigning it the subTotal that was returned from my class?

  17. #17
    Member
    Join Date
    Apr 2014
    Posts
    219
    Thanks
    8
    Thanked 2 Times in 2 Posts

    Default Re: My program is not displaying the answer :(

    in your getter method you just declared another variable but assigned it a double, and your global variable were a float. If you do something like
    private double sales;
    This is a global variable because it can be accessed by all of your methods. There in your getter method you need something like

    if.foo.isSelected()
    regular=3.55;  // this just set the private variable to 3.55

    not
    if.foo.isSelected()
    double regular=3.55; // this just creates another double and never uses it.

    Are you using all of your methods? Write some println statements to see what is working and what it is doing.

    Consider using inheritance. Your programs dependencies with be easier to understand.

  18. #18
    Senior Member
    Join Date
    Jul 2013
    Location
    Europe
    Posts
    666
    Thanks
    0
    Thanked 121 Times in 105 Posts

    Default Re: My program is not displaying the answer :(

    Quote Originally Posted by jocdrew21 View Post
    in your getter method you just declared another variable but assigned it a double, and your global variable were a float. If you do something like
    private double sales;
    This is a global variable because it can be accessed by all of your methods.
    That is NOT a global variable. Please dont teach people wrong terminology.
    This is a private member variable or a "field". You could also call it an "attribute" but definitely not a global.

  19. #19
    Member
    Join Date
    Apr 2014
    Posts
    219
    Thanks
    8
    Thanked 2 Times in 2 Posts

    Default Re: My program is not displaying the answer :(

    Yes cornix is correct wrong terminology....

Similar Threads

  1. Rock paper scissors program not displaying winner
    By namenamename in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 5th, 2013, 03:38 AM
  2. Replies: 3
    Last Post: March 9th, 2013, 07:22 PM
  3. [SOLVED] Need help with displaying items with my program
    By mike101290 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: July 21st, 2011, 08:07 AM
  4. my code displaying some minor wrong o/p ....plz suggest me an answer !
    By naved in forum What's Wrong With My Code?
    Replies: 3
    Last Post: June 28th, 2011, 11:59 AM
  5. [SOLVED] Problem with a tutorial program(Adding the answer of two squared numbers together)
    By Melawe in forum What's Wrong With My Code?
    Replies: 20
    Last Post: April 7th, 2010, 09:03 AM