3 Replies - 1035 Views - Last Post: 26 February 2009 - 09:28 PM Rate Topic: -----

#1 buniwai  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 67
  • Joined: 10-December 08

Having problems with my DecimalFormat

Posted 26 February 2009 - 08:55 PM

I am getting a red line under the DecimalFormat formatter = new DecimalFormat("$###,###..00);

And System.out.println(item.toString() + " Quantity: " + item.getQuantityOnHand() + " Value of inventory: " + formatter.Format(item.getItemValue())); }

The error is Cannot find symbol Symbol : methodFormat(double) location: Class DecimalFormat Surround wirh...

Here is my code:

public class Inventory {
		
		


int inventorySize = 30;
private Product items[] = new Product[inventorySize];

DecimalFormat formatter = new DecimalFormat("$##,###.00");
	
	
//adds the product to the array

public void addProduct(Product item){
	for (int i  = 0; i< inventorySize; i++){
		if (items[i] == null){
		items[i] = item;
		return;

		}
	}
}

public double getTotalInvValue() {
	  double sumOfInventory = 0.0;

	 for (Product item : items){

	  if (item != null) {
		 sumOfInventory += item.getItemValue();
	 }
 }
 return sumOfInventory;
}

public void printInventory(){
	System.out.println("Print Items in Inventory...\n ");

	boolean hasItems = false;

	for (Product item : items){
	if (item != null) {
	  hasItems = true;
	
	
		// If no items were found, print a message saying the inventory is empty.
	 if (!hasItems) { System.out.println("Inventory is empty at the moment.\n"); 
	 }
	 }

	 
	



public class Product {

	/**
	 * @param args the command line arguments
	 */
	public static void main(String[] args) {
		// TODO code application logic here
	

}
	//Private variables
	private String name;
	private int quanity;
	private double price;
	private int productid = 0;

  //default constructor
  //default state of the object

	public Product() {
	  this(0,"UNknown",0,0.00);
	}

 //constructor for name, amount, ID and Price
   public Product(int productId, String itemname, int quanityOnHand, double itemprice){
	   productid = productId;
	   setName(itemname);
	   setQuanityOnHand(quanityOnHand);
	   setPrice(itemprice);
   }

	public void setName(String itemName) {
		name = itemName;
	}
		//quality on hand, if negative then defaults back to 0

	public void setQuanityOnHand(int quanityOnHand){
		if (quanityOnHand >0){
			quanity = quanityOnHand;
	}
		else {quanity = 0;}

	}

//quality on hand and if negative the quality defaults to zero
	public void setPrice(double itemPrice){
	   if (itemPrice >0.00){
		  price = itemPrice;
	   }
	   else {price = 0.00; }
	   }


	   //Retrieve Product name
	 public String getName() {
		return name;
	 }
   public int getQuantityOnHand() {
		return quanity;
   }

   public double getPrice() {
		return price;
	}

   //calculate the Value of the inventory
  public double getItemValue() {

		return (price * (double)quanity);
	}
	   // String representation of the product
	public String toString() {
		return name + " - " + price;
	}
}




Thanks in advance everyone

buni

Is This A Good Question/Topic? 0
  • +

Replies To: Having problems with my DecimalFormat

#2 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8343
  • View blog
  • Posts: 31,890
  • Joined: 06-March 08

Re: Having problems with my DecimalFormat

Posted 26 February 2009 - 09:12 PM

Indent your code correctly to will see that you have a few }}}} missing
Once that corrected it compiles without problem

However you code can't run... there is nothing in the main() method of the Product class
actually I think that the main() method should be in the Inventory class:
- logically Inventory calls Product (an Inventory has products) not the contrary (Product knows nothing about Inventory)
Was This Post Helpful? 0
  • +
  • -

#3 n8wxs  Icon User is offline

  • --... ...-- -.. . -. ---.. .-- -..- ...
  • member icon

Reputation: 972
  • View blog
  • Posts: 3,878
  • Joined: 07-January 08

Re: Having problems with my DecimalFormat

Posted 26 February 2009 - 09:25 PM

In the code you've posted DecimalFormat formatter = new DecimalFormat("$##,###.00"); doesn't produce any errors. Additionally, the code doesn't contain any usage of the formatter variable.
Was This Post Helpful? 0
  • +
  • -

#4 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8343
  • View blog
  • Posts: 31,890
  • Joined: 06-March 08

Re: Having problems with my DecimalFormat

Posted 26 February 2009 - 09:28 PM

n8wxs is right ... as usual....
here is a version of you code that compiles whit all the {} balanced

but as I said it will compile but not run

public class Product {

	/**
	 * @param args the command line arguments
	 */
	public static void main(String[] args) {
		// TODO code application logic here
	

}
	//Private variables
	private String name;
	private int quanity;
	private double price;
	private int productid = 0;

  //default constructor
  //default state of the object

	public Product() {
	  this(0,"UNknown",0,0.00);
	}

//constructor for name, amount, ID and Price
   public Product(int productId, String itemname, int quanityOnHand, double itemprice){
	   productid = productId;
	   setName(itemname);
	   setQuanityOnHand(quanityOnHand);
	   setPrice(itemprice);
   }

	public void setName(String itemName) {
		name = itemName;
	}
		//quality on hand, if negative then defaults back to 0

	public void setQuanityOnHand(int quanityOnHand){
		if (quanityOnHand >0){
			quanity = quanityOnHand;
	}
		else {quanity = 0;}

	}

//quality on hand and if negative the quality defaults to zero
	public void setPrice(double itemPrice){
	   if (itemPrice >0.00){
		  price = itemPrice;
	   }
	   else {price = 0.00; }
	   }


	   //Retrieve Product name
	 public String getName() {
		return name;
	 }
   public int getQuantityOnHand() {
		return quanity;
   }

   public double getPrice() {
		return price;
	}

   //calculate the Value of the inventory
  public double getItemValue() {

		return (price * (double)quanity);
	}
	   // String representation of the product
	public String toString() {
		return name + " - " + price;
	}

}



import java.text.DecimalFormat;

public class Inventory {

	int inventorySize = 30;
	private Product items[] = new Product[inventorySize];

	DecimalFormat formatter = new DecimalFormat("$##,###.00");


//	adds the product to the array

	public void addProduct(Product item){
		for (int i  = 0; i< inventorySize; i++){
			if (items[i] == null){
				items[i] = item;
				return;

			}
		}
	}

	public double getTotalInvValue() {
		double sumOfInventory = 0.0;

		for (Product item : items){

			if (item != null) {
				sumOfInventory += item.getItemValue();
			}
		}
		return sumOfInventory;
	}

	public void printInventory(){
		System.out.println("Print Items in Inventory...\n ");

		boolean hasItems = false;

		for (Product item : items){
			if (item != null) {
				hasItems = true;


				// If no items were found, print a message saying the inventory is empty.
				if (!hasItems) { System.out.println("Inventory is empty at the moment.\n"); 
				}
			}
		}
	}
}


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1