error in compiling inventory program

  • (2 Pages)
  • +
  • 1
  • 2

15 Replies - 1316 Views - Last Post: 14 November 2011 - 10:58 PM Rate Topic: -----

#1 dwheeler0603  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 21
  • Joined: 03-November 11

error in compiling inventory program

Posted 03 November 2011 - 10:59 PM

Hello this is my first time posting on this site. If I violate any rules please tell me to avoid future violations. So, my task is to complete a very basic inventory program that requires zero user input, it must have an inventory class and a product class(product of my choosing, monitors) here is my code:


//class to get and set variables for inventory program
//product is monitors
//created by DW
//last modified 11/03/11

class Monitor {
	private double itemNumber;
	private String productName;
	private double unitsInStock;
	private double price;



	//constructor
	public Monitor ( double ItemNumber, String ProductName, double UnitsInStock, double Price) {
		setItemNumber(itemNumber);
		setProductName(productName);
		setUnitsInStock(unitsInStock);
		setPrice (price);
	}

	//accessor methods for class variables
	public double getItemNumber () {
		return itemNumber;
	}

	public void setItemNumber(double itemNumber) {
		this.itemNumber = itemNumber;
	}

	public String getProductName () {
		return productName;
	}

	public void setProductName (String productName) {
		this.productName = productName;
	}

	public double getUnitsInStock() {
		return unitsInStock;
	}

	public void setUnitsInStock (double price) {
		this.unitsInStock = unitsInStock;
	}
	public double getPrice() {
		return price;
	}

	public void setPrice (double price) {
		this.price = price;
	}
}


//main class for inventory program
//this program displays monitor inventory information
//created by DW
//last modified 11/03/11

import java.text.NumberFormat;			// Program Uses Class NumberFormat
import java.util.Locale;				// Program Needs to Use U.S. Currency

public class InventoryPart1
{

public static void main (String args[]) {

	NumberFormat currency = NumberFormat.getCurrencyInstance(Locale.US);//currency selection U.S. Dollar

Monitor myMonitor = new Monitor("10", "Acer LCD", "23", "435.20");

					double ItemNumber = myMonitor.getItemNumber();
					String ProductName = myMonitor.getProductName();
					double UnitsInStock = myMonitor.getUnitsInStock();
					double Price = myMonitor.getPrice();

					double TotalPrice = UnitsInStock * Price;





				{
									System.out.print("Item number: " + ItemNumber);
									System.out.print("Product name: " + ProductName);
									System.out.print("Units in stock: " + UnitsInStock);
									System.out.print("Price per unit: $ " + currency.format(Price));
									System.out.print("Total price: $ " + currency.format(TotalPrice));

				}
			}
When I compile I recieve one error in my main class (secondary class Monitor compiles fine) I recieve the error message: F:\InventoryPart1.java:16: constructor Monitor in class Monitor cannot be applied to given types;
Monitor myMonitor = new Monitor("10", "Acer LCD", "23", "435.20");
^
required: double,String,double,double
found: String,String,String,String
reason: actual argument String cannot be converted to double by method invocation conversion
1 error

Tool completed with exit code 1

This project is an assignment for an intro to Java course. I find the textbook for this course to be useless. If anyone can recomend a good book on Java I would appreciate it. Thanks for all the help.

Is This A Good Question/Topic? 0
  • +

Replies To: error in compiling inventory program

#2 Sheph  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 447
  • View blog
  • Posts: 1,032
  • Joined: 12-October 11

Re: error in compiling inventory program

Posted 03 November 2011 - 11:12 PM

Your constructor accepts 3 double values and 1 String value. You make all the values Strings by eclosing them in quotations. Just remove the quotations for arguments 1, 3 and 4.

I also give you some acknowledgement for using code tags on your first post. :)
Was This Post Helpful? 1
  • +
  • -

#3 dwheeler0603  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 21
  • Joined: 03-November 11

Re: error in compiling inventory program

Posted 03 November 2011 - 11:20 PM

Thanks for the tip! your suggestion enabled my program to compile without any errors. However, when it runs the application it returns the product name as null and the values as 0. can anyone point me in the right direction to achieve my desired output of: product name = Acer LCD, units in stock = 23, price, 435.20, and total inventory value = Price * unit in stock?
Was This Post Helpful? 0
  • +
  • -

#4 dwheeler0603  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 21
  • Joined: 03-November 11

Re: error in compiling inventory program

Posted 03 November 2011 - 11:27 PM

Sweet, I figured out my final question, thanks for your time. If there is a way to close this forum or mark it as solved please let me know, I do not want to waste anyone's time. Thank you
Was This Post Helpful? 0
  • +
  • -

#5 dwheeler0603  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 21
  • Joined: 03-November 11

Re: error in compiling inventory program

Posted 03 November 2011 - 11:41 PM

Sorry, I have encountered a new problem. My program compiles, runs, and displays the expected results. However, I am having a problem with spacing. The program displays my results as a single sentence. I would like it to appear as:
Item number:10
Product name: Acer Lcd
etc.
my second class Monitor has not changed. here is my updated code for my main class:
//main class for inventory program
//this program displays monitor inventory information
//created by DW
//last modified 11/03/11

import java.text.NumberFormat;			// Program Uses Class NumberFormat
import java.util.Locale;				// Program Needs to Use U.S. Currency

public class InventoryPart1
{

public static void main (String args[]) {

	NumberFormat currency = NumberFormat.getCurrencyInstance(Locale.US);//currency selection U.S. Dollar

Monitor myMonitor = new Monitor(10, "Acer LCD", 23, 435.20);

					double ItemNumber = myMonitor.getItemNumber();
					String ProductName = myMonitor.getProductName();
					double UnitsInStock = myMonitor.getUnitsInStock();
					double Price = myMonitor.getPrice();

					double TotalPrice = UnitsInStock * Price;





				{
									System.out.print("Item number: " + ItemNumber);

									System.out.print("Product name: " + ProductName);

									System.out.print("Units in stock: " + UnitsInStock);

									System.out.print("Price per unit: $ " + currency.format(Price));

									System.out.print("Total price: $ " + currency.format(TotalPrice));

				}
			}

Was This Post Helpful? 0
  • +
  • -

#6 Sheph  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 447
  • View blog
  • Posts: 1,032
  • Joined: 12-October 11

Re: error in compiling inventory program

Posted 03 November 2011 - 11:56 PM

Change your System.out.print() statement to System.out.println(). println stands for Print Line. It will append a newline character to the end of the String. The other option is to stick with print and manually append a "\n" to the end of your String, but generally, println() is the the preferred way to go.
Was This Post Helpful? 1
  • +
  • -

#7 dwheeler0603  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 21
  • Joined: 03-November 11

Re: error in compiling inventory program

Posted 04 November 2011 - 12:02 AM

Thank you, the \n worked, but the printIn did not work for me for some reason.
Was This Post Helpful? 0
  • +
  • -

#8 Sheph  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 447
  • View blog
  • Posts: 1,032
  • Joined: 12-October 11

Re: error in compiling inventory program

Posted 04 November 2011 - 12:29 AM

It's printLn(), but the L is lowercase.
Was This Post Helpful? 0
  • +
  • -

#9 dwheeler0603  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 21
  • Joined: 03-November 11

Re: error in compiling inventory program

Posted 12 November 2011 - 08:13 PM

i am trying to create an inventory program for a basic java class. the requirements are for it to have two classes and a subclass. The program must use an array to store the data type. my class titled monitor should create two products and there info and the subclass should create 3 products and display an additional piece of information, I chose screen size. When I compile I recieve an error message saying the file ended while parsing, I cannot find a bracket out of place, any help, tips, or suggestions would be appreciated. here is my code:
public class Monitor7
{
  int itemNumber[] = new int[2];   //item number
  String productName[] = new String [2]; //product name
  double unitsInStock[] = new double [2];	//units in stock
  double unitPrice[] = new double[2];    //unit price

  public Monitor7(int itemNumber, String productName, double unitsInStock, double unitPrice)//constructor
  {
     this.itemNumber = "4, 5";
     this.productName = "AcerJD, Apple13";
     this.unitsInStock = "6, 4";
     this.unitPrice = "110.14, 300.00";
     }
//	total value of inventory
	public double calculateInventory() {
		return unitsInStock * unitPrice;
	}

//	getters and setters

	public int getItemNumber() {
		return itemNumber;
	}

	public void setItemNumber(int itemNumber) {
		this.itemNumber = itemNumber;
	}

	public String getProductName() {
		return productName;
	}

	public void setProductName(String productName) {
		this.productName = productName;
	}

	public int getUnitsInStock() {
		return unitsInStock;
	}

	public void setUnitsInStock(int unitsInStock) {
		this.unitsInStock = unitsInStock;
	}

	public double getUnitPrice() {
		return unitPrice;
	}

	public void setUnitPrice(double unitPrice) {
		this.unitPrice = unitPrice;
	}

class Dell extends Monitor7	{

private int screenSize = "";

	public Dell(int itemNumber, String productName, double unitsInStock, double unitPrice, double screenSize) {
		super(itemNumber, productName, unitsInStock, unitPrice);
		this.screenSize = screenSize;
	}

	public double getScreenSize() {
		return screenSize;
	}

	public void setScreenSize(double screenSize) {
		this.screenSize = screenSize;
	}

//	total value
	public double getInventoryValue() {
		return getQuantity()*getPrice();
	}

//	restock fee
	public double calculateRestockFee() {
		return 0.05*getPrice();
	}

//	toString returns the printout of the individual item for display to the screen
	public String toString()
	{
		return String.format("\n%s %s\n%s\t %s\n%s\t %d\n%s\t $%,.2f\n%s\t%s\n%s\t $%,.2f \n%s\t$%,.2f","Product Number: ", getdvdNumber(),
				"Product Name: ", getproductName(), "Units In Stock:", getUnitsInStock(),
				"Price per Unit:", getUnitPrice(),
				"Screen size:", getScreenSize(),
				"Fee:", fee(), "Total Inventory Value:", getInventoryValue()); }


}


here is my main class:
// stores an inventory
public class InventoryProgram7 {
	private static InventoryProgram7 i;

	public static void main(String[] args) {

		// create 3 products
		Dell p1 = new Dell(1, "LCDJ7", 3, 14.99, "12 inches");
		Dell p2 = new Dell(2, "LCDXP9", 5, 19.99, "22 inches");
		Dell p3 = new Dell(3, "LCDQ4", 4, 18.99, "18 inches");

		// make inventory
		i = new Inventory(3);
		i.add(p1, 0);
		i.add(p2, 1);
		i.add(p3, 2);

		// sort
		i.sort();

		// output
		for (int k = 0; k < 3; k++) {
			System.out.println(i.get(k));
		}

		System.out.println();

		// total val
		System.out.printf("Total value = $%.2f", i.totalValue());
	}

}

also, I am new to arrays, please let me know if I am using them incorrectly. Thank you
Was This Post Helpful? 0
  • +
  • -

#10 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2250
  • View blog
  • Posts: 5,340
  • Joined: 10-September 10

Re: error in compiling inventory program

Posted 13 November 2011 - 03:04 AM

You posted a good explanation of what you have to do, a little too much code - especially for this problem, and an incomplete error message. Don't interpret or paraphrase error messages. Copy and paste them exactly as they appear to you into your post.

It's interesting that you said you couldn't find a bracket out of place. Is that the typical root cause of your problems? If so, why not use an editor that helps you keep brackets matched? If you can't change editors (or learn to use that feature in the one you're using), then it might help you to NEVER, NEVER, NEVER create an open bracket without creating its closing partner.

Your error message means that the compiler is expecting more source code, but the file ended. That suggests that you have fewer closing brackets than you have open brackets in your source code. Recheck that. Again, I suggest using an editor that helps point out those basic syntactical errors.

Good luck and let us know if you need more help.
Was This Post Helpful? 1
  • +
  • -

#11 smohd  Icon User is offline

  • Critical Section
  • member icon


Reputation: 1822
  • View blog
  • Posts: 4,627
  • Joined: 14-March 10

Re: error in compiling inventory program

Posted 13 November 2011 - 03:15 AM

Your int itemNumber[] = new int[2]; is an array of int but you tried to assign a string here:
this.itemNumber = "4, 5"; and many of those problems in productName and other instance variables. What are you trying to change?
Was This Post Helpful? 1
  • +
  • -

#12 dwheeler0603  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 21
  • Joined: 03-November 11

Re: error in compiling inventory program

Posted 13 November 2011 - 06:04 PM

Well I got my code to work after a lot of editing. However, I still have two problems. First i would like all my values of price to display as U.S. currency, I am used to using the format.currency but, it is not working for me in this case. Secondly I would like my program to display as a downward list rather than in one long line as it does now. here is my code:
// This is the Driver program for Product

import java.text.NumberFormat;			// Program Uses Class NumberFormat
import java.util.Locale;				// Program Needs to Use U.S. Currency

public class InventoryPart2_3 {

	NumberFormat currency = NumberFormat.getCurrencyInstance(Locale.US);//currency selection U.S. Dollar

	public static void main(String[] args) {
		BigScreenMonitor p1 = new BigScreenMonitor(156779, "Dell LCD XJV", 3, 145.99, "24 In.");
		BigScreenMonitor p2 = new BigScreenMonitor(1568232, "Apple Ultra HighDEF LCD AJJ", 5, 299.99, "32 In.");
		BigScreenMonitor p3 = new BigScreenMonitor(1569663, "Acer LCD AXD", 4, 189.99, "26 In.");
		Monitor p4 = new Monitor(186674, "Samsung SxJ LCD",6, 110.29);
		Monitor p5 = new Monitor(186875, "HP LCD MS", 7, 84.99);

		// Create Array to store the objects
		Monitor[] objArray = new Monitor[5];
		objArray[0] = p1;
		objArray[1] = p2;
		objArray[2] = p3;
		objArray[3] = p4;
		objArray[4] = p5;

		sortMonitors(objArray);
		// Print the values after sorting
		for (int i = 0; i < objArray.length; i++) {
			System.out.println(objArray[i].toString());
			// Print blank line
			System.out.println();
		}

		double totalInventoryValue = calculateEntireInventory(objArray);
		System.out.println("Value of entire inventory: $" + totalInventoryValue);

	}

	/**
	 * This method is used to sort the products by Product Name
	 *
	 * @param dvdArray
	 */
	private static void sortMonitors(Monitor[] monitorArray) {
		int length = monitorArray.length;
		Monitor obj1 = null;
		Monitor obj2 = null;
		Monitor temp = null;
		for (int i = 1; i < length; i++) {
			for (int j = 0; j < length - i; j++) {
				obj1 = monitorArray[j];
				obj2 = monitorArray[j + 1];
				if (obj1.getProductName().compareTo(obj2.getProductName()) > 0) {
					temp = obj1;
					monitorArray[j] = monitorArray[j + 1];
					monitorArray[j + 1] = temp;
				}
			}
		}
	}

	/**
	 * This method will be used to calculate the Entire Inventory Value
	 *
	 * @param dvdArray
	 * @return
	 */
	private static double calculateEntireInventory(Monitor[] monitorArray) {
		Monitor obj = null;
		double totalInventory = 0;
		for (int i = 0; i < monitorArray.length; i++) {
			// Sets the current object in obj variable
			obj = monitorArray[i];
			totalInventory += obj.getInventoryValue();
		}
		return totalInventory;
	}

}

public class Monitor {
	private int itemNumber;
	private String productName;
	private int unitsInStock;
	private double unitPrice;

	// default no arg constructor
	public Monitor() {
		this.itemNumber = 0;
		this.productName = "unknown";
		this.unitsInStock = 0;
		this.unitPrice = 0;
	}

	public Monitor(int number, String name, int quantity, double MSRP) {
		itemNumber = number;
		productName = name;
		this.unitsInStock = quantity;
		this.unitPrice = MSRP;
	}

	// calculates inventory value
	// number of units in stock multiplied by the
	// price of eachunit
	public double getInventoryValue() {
		return unitPrice * unitsInStock;
	}

	public String toString() {
		String str = "Number: " + getItemNumber();
		str += ", Name: " + getProductName();
		str += ", In Stock: " + getUnitsInStock();
		str += ", Unit Price: " + getUnitPrice() + "\n";
		str += "Inventory Value: = $" + getInventoryValue() + "\n";
		return str;
	}

	public int getItemNumber() {
		return itemNumber;
	}

	public void setItemNumber(int itemNumber) {
		this.itemNumber = itemNumber;
	}

	public String getProductName() {
		return productName;
	}

	public void setProductName(String productName) {
		this.productName = productName;
	}

	public int getUnitsInStock() {
		return unitsInStock;
	}

	public void setUnitsInStock(int unitsInStock) {
		this.unitsInStock = unitsInStock;
	}

	public double getUnitPrice() {
		return unitPrice;
	}

	public void setUnitPrice(double unitPrice) {
		this.unitPrice = unitPrice;
	}

}

class BigScreenMonitor extends Monitor {

	private String size = "";

	public BigScreenMonitor(int item, String name, int units, double price, String size) {
		super(item, name, units, price);
		this.size = size;
	}

	public String getSize() {
		return size;
	}

	public void setSize(String size) {
		this.size = size;
	}

	// calculates inventory value after adding restocking fee
	public double getInventoryValue() {
		double value = super.getInventoryValue();
		// Add 5% restocking fee
		value = value + getRestockingFee();
		return value;
	}

	// Restocking fee is 5% of value
	public double getRestockingFee() {
		double value = super.getInventoryValue();
		return value * 0.05;
	}

	public String toString() {
		String str = super.toString();
		str += "Size: " + getSize() + "\n";
		return str;
	}

}


Was This Post Helpful? 0
  • +
  • -

#13 dwheeler0603  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 21
  • Joined: 03-November 11

Re: error in compiling inventory program

Posted 13 November 2011 - 09:01 PM

I got my spacing issue figured out, still trying to display in currency
Was This Post Helpful? 0
  • +
  • -

#14 smohd  Icon User is offline

  • Critical Section
  • member icon


Reputation: 1822
  • View blog
  • Posts: 4,627
  • Joined: 14-March 10

Re: error in compiling inventory program

Posted 14 November 2011 - 02:05 AM

When you first start this topic I was thinking you are doing a different thing now. But at the end you return back to where your first thread was.
*Topic with the same issues and code merged*


You have already created a NumberFormat object, so what to do now is just using a format method to format your object which you need to format. For example, you can say:
System.out.println(currency.format(2.3)); //this will print a formatted number
the output of that line will be $2.30.

You can also use the NumberFormat object to parse your string. Look more in NumberFormat class documentation

This post has been edited by smohd: 14 November 2011 - 02:08 AM

Was This Post Helpful? 1
  • +
  • -

#15 dwheeler0603  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 21
  • Joined: 03-November 11

Re: error in compiling inventory program

Posted 14 November 2011 - 10:21 PM

thanks for the info on curencyformat I was actually able to figure it out on my own after browsing the sun website. Should have looked there first but I was getting close to my deadline and got stressed :dangole:
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2