Can someone please tell me why "int can not be dereferenced" here.
contents += cart.toString() + "\n


// **********************************************************************
//   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.
    // -----------------------------------------------------------
    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)
    {
    }

    // -------------------------------------------------------
    //  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";

	int[] cart = new int [capacity];
        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
    // ---------------------------------------------------------
    //Fill in the code for the increaseSize method. Your code should be
    //similar to that in Listing 7.8 of the text but instead of doubling the
    //size just increase it by 3 elements. 
    private void increaseSize()
    {
        int[] cart = new int [capacity + 3];
        
    }
}

Recommended Answers

All 7 Replies

try using Integer instead of int, and btw, I think you don't need to use .toString() method.

I don't understand what you are trying to say.

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

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

First of all, I think you missed a " after the \n inside the loop.
Secondly, cart is an int. Where did you see written that ints have methods?
The cart doesn't have a method toString because it is an int. You can't make up from your mind methods and call them and then wonder why the code doesn't run.

This is going to work:

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

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

First of all, I think you missed a " after the \n inside the loop.
Secondly, cart is an int. Where did you see written that ints have methods?
The cart doesn't have a method toString because it is an int. You can't make up from your mind methods and call them and then wonder why the code doesn't run.

This is going to work:

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

cart is supposed to be an array. Am I doing arrays properly? I can't find very much documentation on arrays.

the array is done properly, however, you need to think about
the data type, which is the problem here.

try this, it will help you understand:

int x = 0;
System.out.println(x.toString());

now try this:

Integer x = 0;
System.out.println(x.toString());

the reason that the second code worked is because of the data type.
primitive data types like int do not have methods, unlike Integer data type.
you may want to read about primitive data types in Java.

And also make sure to read about Classes in Java and Objects in Java. int, double, float, boolean, long, short, etc can not use method invocations ( like someInt.someMethod() ) because they are not class types!!!

cart is supposed to be an array. Am I doing arrays properly? I can't find very much documentation on arrays.

And one more clarification. You might already know, and I might look silly telling it to you it is very important and lots of people do that mistake:

cart is supposed to be an array.

cart is not an array. cart is an int. cart is an array.

Meaning that cart should be treated like an array. You can call the methods of an array like .length.
But cart is an int and can be used in any other way you could use an int:

void method(int arg) {

}

method(cart[i]);
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.