Java project output problem. Can't output objects from a method.

In summary, make sure to check the getInput() and displayOutput() methods, as well as the design of your program, to properly output the variables and objects from getInput(). Consider seeking help if you are still having trouble.
  • #1
clook
35
0
I'm trying to create a bookstore calculation program with two classes, a presentation and computation class. Here is my code for the presentation class:
Code:
import javax.swing.*;
import java.awt.event.*;
import java.text.*;
import java.awt.*;

public class ch4Book extends JFrame implements ActionListener
{

	// All the GUI objects
	JPanel mainPanel = new JPanel();
	JLabel companyLabel = new JLabel("       OrderBook Inc.         ");
	JTextField titleTextField = new JTextField(30);
	JTextField quantityTextField = new JTextField(15);
	JTextField priceTextField = new JTextField(15);
	JButton calculateButton = new JButton(" Calculate   ");
	JTextArea outputTextArea = new JTextArea("Your Total Cost:", 10, 25);
	JScrollPane outputScrollPane = new JScrollPane(outputTextArea);
	

	
	
	public static void main(String[] args)
	{
		//create an object of the class and code the close button
		ch4Book myCost = new ch4Book();
		myCost.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	}
	
	public ch4Book()
	{
		designFrame();
	}
	
	public void designFrame()
	{
		//add the components to the mainPanel
		mainPanel.add(companyLabel);
		mainPanel.add(new JLabel("       Title            "));
		mainPanel.add(titleTextField);
		mainPanel.add(new JLabel("    Quantity   "));
		mainPanel.add(quantityTextField);
		mainPanel.add(new JLabel("    Price       "));
		mainPanel.add(priceTextField);
		mainPanel.add(calculateButton);
		mainPanel.add(outputScrollPane);
		
		//add the listeners
		calculateButton.addActionListener(this);
		
		
		//add the panel to the frame
		add(mainPanel);
		setSize(500,500);
		setVisible(true);
		
	}
	
	public void actionPerformed(ActionEvent evt)
	{
		getInput();
		displayOutput();
		clear();
		
	}
	
	public void getInput()
	{
		//input variables

		double  priceDouble;
		int quantityInteger;
		String titleString;
		

		priceDouble = Double.parseDouble(priceTextField.getText());
		quantityInteger = Integer.parseInt(quantityTextField.getText());
		titleString = titleTextField.getText();
		
	    
		//send the input the calculation class
		Cost myTotalCost = new Cost(priceDouble, quantityInteger);
		displayOutput();
		
		
	}
	
	public void displayOutput()
	{
		// object to format to currency
		DecimalFormat formatDecimalFormat = new DecimalFormat("$0.00");
		
		//variables to store the retrieved values from the computation class
		double  subTotalDouble, totalCostDouble;
		int counterInteger;
		
		Cost myTotalCost = new Cost();
	    
	    subTotalDouble = myTotalCost.getSubTotal();
	    totalCostDouble = myTotalCost.getTotalCost();
	    counterInteger = myTotalCost.getCounterInteger();
	    
	    
	    outputTextArea.append('\n' + "Book Title:  " + titleString +
	    		'\n' + " Quantity  " + formatDecimalFormat.format(quantityInteger)+
	    		'\n' + "Number of Customers  " + counterInteger);
	    
		
	}
	
	public void clear()
	{
		titleTextField.setText("");
		quantityTextField.setText("");
		priceTextField.setText("");
	}
}

Problem is, I'm unable to output the variables and objects from getInput()

I'm not sure why.. can someone help me with this?
 
Physics news on Phys.org
  • #2


Hi there,

I would recommend taking a step back and breaking down the problem into smaller parts. First, make sure that your getInput() method is properly receiving and storing the input values. You can do this by printing out the values you are retrieving from the text fields and checking if they match the user inputs.

Next, make sure that your displayOutput() method is properly accessing the values from the computation class and displaying them in the output text area. Again, you can print out the values to check if they are being retrieved correctly.

If these two parts are working correctly, then the issue may lie in your designFrame() method or in the way you are calling the methods in your actionPerformed() method. Double check these areas and make sure everything is connected properly.

If you are still having trouble, consider reaching out to a fellow programmer or seeking help from a programming forum. Sometimes having a fresh pair of eyes can help identify the problem. Good luck with your project!
 
  • #3


It seems like you have not properly declared and initialized your variables in the displayOutput() method. Make sure you have declared and initialized the variables subTotalDouble, totalCostDouble, and counterInteger before trying to output them in your outputTextArea. Also, make sure you are calling the displayOutput() method after calling the getInput() method in your actionPerformed() method.
 

1. Why am I not able to output objects from my Java project?

There could be several reasons why you are facing this problem. Firstly, ensure that you have properly imported all necessary libraries and packages for your project. Additionally, check that the objects you are trying to output are properly instantiated and have the necessary access modifiers. It is also important to make sure that your output statement is properly coded and is being called from the correct location in your code.

2. What does it mean when I receive an error message saying "cannot find symbol" when trying to output objects?

This error message usually means that the compiler is unable to recognize the variable or object you are trying to output. Make sure that the object is properly declared and instantiated, and that you are using the correct variable name in your output statement.

3. I am able to output simple data types, but not objects. Why is this happening?

Objects require a different approach when it comes to outputting their values. Make sure that you are using the correct method for outputting objects, such as toString() or getter methods. Also, check that your objects have proper string representations or else they may not display correctly when outputted.

4. Can I output multiple objects at once?

Yes, it is possible to output multiple objects at once by using concatenation or formatting methods. However, make sure that all the objects you are trying to output have the appropriate string representation or else the output may not make sense.

5. Why is my program only outputting null when I try to output objects?

This could be happening for a few reasons. One possibility is that the object you are trying to output was never properly instantiated, so it has a null value. Another reason could be that the object's string representation is not properly defined, causing it to display as null when outputted. Make sure to check your code for any missed or incorrect instantiations and string representations.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
2
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
5
Views
4K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
6K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
5K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
2K
  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
Replies
2
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
9
Views
3K
Back
Top