[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Invemtory program with Java Swing GUI gives me one error I can't debug

Posted on 2009-04-15
2
Medium Priority
?
924 Views
Last Modified: 2013-11-23
All,

The code below gives me this one error I can't seem to figure out:

Inventory5.java:239: cannot resolve symbol
symbol  : method format  (java.lang.String,java.lang.String,int,int,double,java.
lang.String,double,double)
location: class java.lang.String
                return String.format("Product: %s\nItem Number: %d\nIn Stock: %d
\nPrice: $%.2f\nType: %s\nTotal Value of Stock: $%.2f\nRestock Cost: $%.2f\n\n\n
",
                             ^
1 error


Essentially I'm trying to adding a button to the GUI that allows the user to move to the first item, the previous item, the next item, and the last item in the inventory. If the first item is displayed and the and the user clicks on the previous button, the last item should display. If the last item is displayed and the user clicks on the Next button, the first item should display.
Add a company logo to the GUI using Java graphics classes.

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
 
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
 
public class Inventory5
{
	static int dispProd = 0; // variable for actionEvents
	
	public static void main(final String args[])
	{
 
		int i; // varialbe for looping
		double total = 0; // variable for total inventory
 
 
		// Instantiate a product object
		final ProductAdd[] nwProduct = new ProductAdd[5];
		// Instantiate objects for the array
		for (i=0; i<5; i++)
		{
			nwProduct[0] = new ProductAdd("Quantum of Solace", 101, 10, 24.00, "CD");
			nwProduct[1] = new ProductAdd("Slumdog Millionaire", 102, 10, 25.75, "DVD");
			nwProduct[2] = new ProductAdd("Iron Man", 103, 10, 25.50, "DVD");
			nwProduct[3] = new ProductAdd("The Dark Knight", 104, 10, 25.00, "Box");
			nwProduct[4] = new ProductAdd("Gran Torino", 105, 10, 23.00, "CD");
			
			 
 
			
		}
 
  
			for (i=0; i<5; i++)
				total += nwProduct.length; // calculate total inventory cost
 
			final JButton firstBtn = new JButton("First"); // first button
			final JButton prevBtn = new JButton("Previous"); // previous button
			final JButton nextBtn = new JButton("Next"); // next button
			final JButton lastBtn = new JButton("Last"); // last button
			final JLabel label; // logo
			final JTextArea textArea; // text area for product list
			final JPanel buttonJPanel; // panel to hold buttons
 
			//JLabel constructor for logo
			Icon logo = new ImageIcon("C:/logo.jpg"); // load logo
			label = new JLabel(logo); // create logo label
			label.setToolTipText("Companies Design"); // create tooltip
 
			buttonJPanel = new JPanel(); // set up panel
			buttonJPanel.setLayout( new GridLayout(1, 4)); //set layout
 
			// add buttons to buttonPanel
			buttonJPanel.add(firstBtn);
			buttonJPanel.add(prevBtn);
			buttonJPanel.add(nextBtn);
			buttonJPanel.add(lastBtn);
 
 
			textArea = new JTextArea(nwProduct[3]+"\n"); // create textArea for product display
 
			// add total inventory value to GUI
			textArea.append("\nTotal value of Inventory "+new java.text.DecimalFormat("$0.00").format(total)+"\n\n");
			textArea.setEditable(false); // make text uneditable in main display
			JFrame invFrame = new JFrame(); // create JFrame container
			invFrame.setLayout(new BorderLayout()); // set layout
			invFrame.getContentPane().add(new JScrollPane(textArea), BorderLayout.CENTER); // add textArea to JFrame
			invFrame.getContentPane().add(buttonJPanel, BorderLayout.SOUTH); // add buttons to JFrame
			invFrame.getContentPane().add(label, BorderLayout.NORTH); // add logo to JFrame
			invFrame.setTitle("Office Min Inventory"); // set JFrame title
			invFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // termination command
			invFrame.setSize(400, 400); // set size of JPanel
			invFrame.setLocationRelativeTo(null); // set screem location
			invFrame.setVisible(true); // display window
 
 
 
			// assign actionListener and actionEvent for each button
			firstBtn.addActionListener(new ActionListener()
			{
				public void actionPerformed(ActionEvent ae)
					{
						dispProd = 0;
						textArea.setText(nwProduct[dispProd]+"\n");
 
					} // end firstBtn actionEvent
 
			}
		   ); // end firstBtn actionListener
 
 
	prevBtn.addActionListener(new ActionListener()
	{
		public void actionPerformed(ActionEvent ae)
		{
			dispProd--;
			if (dispProd < 0)
			{
				dispProd = 0;
			}
			
			textArea.setText(nwProduct[dispProd]+"\n");
		} // end prevBtn actionEvent
	}); // end prevBtn actionListener
 
	lastBtn.addActionListener(new ActionListener()
	{
		public void actionPerformed(ActionEvent ae)
		{
			dispProd = nwProduct.length-1;
			textArea.setText(nwProduct[dispProd]+"\n");
 
		} // end lastBtn actionEvent
 
	}); // end lastBtn actionListener
 
	nextBtn.addActionListener(new ActionListener()
 	{
		public void actionPerformed(ActionEvent ae)
		{
			dispProd++;
			if (dispProd >= nwProduct.length)
			{
				dispProd = nwProduct.length-1;
			}
			textArea.setText(nwProduct[dispProd]+"\n");
		} // end nextBtn actionEvent
	}); // end nextBtn actionListener
 
} // end main
 
} // end class Inventory5
 
class Product
{
	protected String prodName; // name of product
	protected int itmNumber; // item number
	protected int units; // number of units
	protected double price; // price of each unit
	protected double value; // value of total units
 
 
	public Product(String name, int number, int unit, double each) // Constructor for class Product
	{
		prodName = name;
		itmNumber = number;
		units = unit;
		price = each;
 
	} // end constructor
 
	public void setProdName(String name) // method to set product name
	{
		prodName = name;
	}
 
	public String getProdName() // method to get product name
	{
		return prodName;
	}
 
	public void setItmNumber(int number) // method to set item number
	{
		itmNumber = number;
	}
 
	public int getItmNumber() // method to get item number
	{
		return itmNumber;
	}
 
	public void setUnits(int unit) // method to set number of units
	{
		units = unit;
	}
 
	public int getUnits() // method to get number of units
	{
		return units;
	}
 
	public void setPrice(double each) // method to set price
	{
		price = each;
	}
 
	public double getPrice() // method to get price
	{
		return price;
	}
 
	public double calcValue() // method to set value
	{
		return units * price;
	}
 
  
} // end class Product
 
 
class ProductAdd extends Product
{
	private String feature; // variable for added feature
 
	public ProductAdd(String name, int number, int unit, double each, String addFeat)
	{
		// call to superclass Product constructor
		super(name, number, unit, each);
 
		feature = addFeat;
	}// end constructor
 
	public void setFeature(String addFeat) // method to set added feature
	{
		feature = addFeat;
	}
 
	public String getFeature() // method to get added feature
	{
		return feature;
	}
 
	public double calcValueRstk() // method to set value and add restock fee
	{
		return units * price * 0.05;
	}
 
	public String toString()
	{
		return String.format("Product: %s\nItem Number: %d\nIn Stock: %d\nPrice: $%.2f\nType: %s\nTotal Value of Stock: $%.2f\nRestock Cost: $%.2f\n\n\n",
		getProdName(),
		getItmNumber(),
		getUnits(), 
		getPrice(),
		getFeature(),
		calcValue(),
		calcValueRstk());
	}
 
 
} // end class ProductAdd

Open in new window

0
Comment
Question by:kpu8
  • 2
2 Comments
 
LVL 92

Expert Comment

by:objects
ID: 24153682
you need to be using java 1.5+ to use that method

0
 
LVL 92

Accepted Solution

by:
objects earned 500 total points
ID: 24153687
Use MessageFormat for earlier versions of java
or upgrade to latest

0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Here is a helpful source code for C++ Builder programmers that allows you to manage and manipulate HTML content from C++ code, while also handling HTML events like onclick, onmouseover, ... Some objects defined and used in this source include: …
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
THe viewer will learn how to use NetBeans IDE 8.0 for Windows to perform CRUD operations on a MySql database.
Suggested Courses
Course of the Month17 days, 17 hours left to enroll

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question