Cannot find symbol error

calling methods from other classes

Page 1 of 1

6 Replies - 1139 Views - Last Post: 10 April 2010 - 10:20 PM Rate Topic: -----

#1 justabeginner  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 36
  • Joined: 17-February 10

Cannot find symbol error

Posted 10 April 2010 - 09:06 PM

I have three different classes. Main is Shopping.java and when I compile this class I am getting the following error:
Shopping.java:35: cannot find symbol
symbol : method ShoppingCart()
location: class Shopping
System.out.println(ShoppingCart());

I have ShoppingCart instantiated within this class so I am not sure why I am getting this error. Please help!!

// ***************************************************************
//   Item.java
//
//   Represents an item in a shopping cart.
// ***************************************************************

import java.text.NumberFormat;

public class Item
{
    private String name;
    private double price;
    private int quantity;

    // -------------------------------------------------------
    //  Create a new item with the given attributes.
    // -------------------------------------------------------
    public Item (String itemName, double itemPrice, int numPurchased)
    {
	name = itemName;
	price = itemPrice;
	quantity = numPurchased;
    }

    // -------------------------------------------------------
    //   Return a string with the information about the item
    // -------------------------------------------------------
    public String toString ()
    {
	NumberFormat fmt = NumberFormat.getCurrencyInstance();

	return (name + "\t" + fmt.format(price) + "\t" + quantity + "\t"
		+ fmt.format(price*quantity));
    }

    // -------------------------------------------------
    //   Returns the unit price of the item
    // -------------------------------------------------
    public double getPrice()
    {
	return price;
    }

    // -------------------------------------------------
    //   Returns the name of the item
    // -------------------------------------------------
    public String getName()
    {
	return name;
    }

    // -------------------------------------------------
    //   Returns the quantity of the item
    // -------------------------------------------------
    public int getQuantity()
    {
	return quantity;
    }
}  

// **********************************************************************
//   ShoppingCart.java
//
//   Represents a shopping cart as an array of items
// **********************************************************************

import java.text.NumberFormat;

public class ShoppingCart
{
    private int itemCount;      // total number of items in the cart
    private double totalPrice;  // total price of items in the cart
    private int capacity;       // current cart capacity

    // -----------------------------------------------------------
    //  Creates an empty shopping cart with a capacity of 5 items.
    // -----------------------------------------------------------
    private Item[] cart = new Item[5];  
	 
	 public ShoppingCart()
    {
	   capacity = 5;
	   itemCount = 0;
	   totalPrice = 0.0;
    }

    // -------------------------------------------------------
    //  Adds an item to the shopping cart.
    // -------------------------------------------------------
    public void addToCart(String itemName, double price, int quantity)
    {
	   if(itemCount==cart.length)
		{
		  increaseSize();
		  capacity += 3;
		}  
		cart[itemCount] = new Item(itemName, price, quantity);
		itemCount++;
	   totalPrice += price*quantity;
		capacity--;
    }

    // -------------------------------------------------------
    //  Returns the contents of the cart together with
    //  summary information.
    // -------------------------------------------------------
    public String toString()
    {
   	NumberFormat fmt = NumberFormat.getCurrencyInstance();

	   String contents = "\nShopping Cart\n";
   	contents += "\nItem\t\tUnit Price\tQuantity\tTotal\n";

   	for (int i = 0; i < itemCount; i++)
	     contents += cart[i].toString() + "\n";

   	contents += "\nTotal Price: " + fmt.format(totalPrice);
   	contents += "\n";

      return contents;
    }

    // ---------------------------------------------------------
    //  Increases the capacity of the shopping cart by 3
    // ---------------------------------------------------------
    private void increaseSize()
    {
	   Item[] temp = new Item[cart.length+3];
		for(int i=0; i<cart.length; i++)
		  temp[i] = cart[i];
		cart=temp;
    }
	
	 // ---------------------------------------------------------
    //  Returns the total price
    // --------------------------------------------------------- 
	 public double getTotalPrice()
	 {
	   return totalPrice;
	 }
}

import java.util.Scanner;

public class Shopping
{
  public static void main (String[] args)
  {
  String ans;
  int quantity;
  double price;
  String itemName;
  
  Scanner scan = new Scanner(System.in);
  ShoppingCart ShopCart = new ShoppingCart();
  
  System.out.print("Do you wish to shop?");
  ans = scan.nextLine();
  
  while (ans.equalsIgnoreCase("y"))
  {
    System.out.print("Enter item: ");
	 itemName = scan.nextLine();
    System.out.print("Enter price: ");
	 price = scan.nextFloat();
	 System.out.print("Enter quantity: ");
	 quantity = scan.nextInt();
	 scan.nextLine();
	 	 
	 //add item to cart
	 ShopCart.addToCart(itemName, price, quantity);
	 System.out.println(ShoppingCart());
	 System.out.println("Please pay " + ShopCart.getTotalPrice());
	} //end of while loop	 
  }
}


Is This A Good Question/Topic? 0
  • +

Replies To: Cannot find symbol error

#2 erik.price  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 485
  • View blog
  • Posts: 2,690
  • Joined: 18-December 08

Re: Cannot find symbol error

Posted 10 April 2010 - 09:14 PM

System.out.println(ShoppingCart());


Your Shopping class doesn't have a ShoppingCart method defined.

Did you mean to do this?
System.out.println(ShopCart.toString());

Was This Post Helpful? 0
  • +
  • -

#3 pdkharkar  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 63
  • View blog
  • Posts: 345
  • Joined: 19-January 09

Re: Cannot find symbol error

Posted 10 April 2010 - 09:16 PM

it is obvious
you have not defined any of the method called shoppingCart in your code
what you have written is a constructor
Was This Post Helpful? 0
  • +
  • -

#4 justabeginner  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 36
  • Joined: 17-February 10

Re: Cannot find symbol error

Posted 10 April 2010 - 09:33 PM

Oh, I tried ShopCart() in my print line, but did not add the toString method. However, when I change this, my program is entering a infinite loop and not asking the user "Do you wish to shop?" after adding an item to ShoppingCart. I checked all my brackets and they seem to be in the correct place, so I am not sure why it is not doing this after it passes through the while loop. Any suggestions??
Was This Post Helpful? 0
  • +
  • -

#5 erik.price  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 485
  • View blog
  • Posts: 2,690
  • Joined: 18-December 08

Re: Cannot find symbol error

Posted 10 April 2010 - 09:39 PM

The toString method will be called implicitly, so yes, this is acceptable too: System.out.println(ShopCart);

You assign 'ans' outside of your loop here: ans = scan.nextLine(); and then never reassign it inside of your while loop, so your condition, while (ans.equalsIgnoreCase("y")) will always be true.

Reassign 'ans' at the end of your while loop
Was This Post Helpful? 1
  • +
  • -

#6 justabeginner  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 36
  • Joined: 17-February 10

Re: Cannot find symbol error

Posted 10 April 2010 - 10:17 PM

I had that written in my notes and just forgot to add it in my code, but it is working properly now. Thanks so much!
Was This Post Helpful? 0
  • +
  • -

#7 erik.price  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 485
  • View blog
  • Posts: 2,690
  • Joined: 18-December 08

Re: Cannot find symbol error

Posted 10 April 2010 - 10:20 PM

No problem! :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1