Welcome to Dream.In.Code
Become a Java Expert!

Join 149,505 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,331 people online right now. Registration is fast and FREE... Join Now!




print statement errors

 
Reply to this topicStart new topic

print statement errors, I am getting cannot find symbol errors in my print statements.

lb33ts
17 Aug, 2007 - 08:17 AM
Post #1

New D.I.C Head
*

Joined: 17 Aug, 2007
Posts: 1


My Contributions
[code]


// Victoria Steele
// class for storing and displaying inventory item

public class Product
{
public int number; // number in inventory
public String title; // name of game in inventory
public int stock; // number of units in stock
public double price; // price of unit in inventory

// constructor for Product class
public Product(int idNumber, String gameTitle, int unitStock, double unitPrice)
{
int number = idNumber;
String title = gameTitle;
int stock = unitStock;
double price = unitPrice;
} // end of constructor

// create objects for inventory
public static void main( String args[] )
{
// create object for first item
Product myProduct1 = new Product( 01, " Madden ", 3, 39.99);

myProduct1.displayInventory();

Product myProduct2 = new Product( 02, " Tiger Woods ", 3, 49.99 );

myProduct2.displayInventory();

Product myProduct3 = new Product( 03, " Grand Theft Auto ", 1, 39.99 );

myProduct3.displayInventory();

Product myProduct4 = new Product( 04, " Destroy All Humans ", 1, 24.99 );

myProduct4.displayInventory();

Product myProduct5 = new Product( 05, " Mafia ", 1, 29.99 );

myProduct5.displayInventory();

} // end main



// method for value anddisplaying members of inventory
public void displayInventory()
{
System.out.print( idNumber + "\t" + gameTitle + "\t" + unitStock + "\t\t" );
System.out.printf("$%.2f\t\t", unitPrice);

} // end display



} // end class Product











User is offlineProfile CardPM
+Quote Post

spullen
RE: Print Statement Errors
17 Aug, 2007 - 08:57 AM
Post #2

D.I.C Regular
Group Icon

Joined: 22 Mar, 2007
Posts: 330



Thanked: 1 times
Dream Kudos: 50
My Contributions
There is no function for System.out.printf
I think the printf function is in the PrintStream class (or somewhere else, go to the java api for a looksky)
User is offlineProfile CardPM
+Quote Post

n_b
RE: Print Statement Errors
17 Aug, 2007 - 09:42 AM
Post #3

New D.I.C Head
*

Joined: 9 Aug, 2007
Posts: 8


My Contributions
Hi there!

I will post two possible solutions to your problem.

If you redefine the displayInventory() method like this:
CODE

public void displayInventory()
{
    System.out.print(number + "\t" + title + "\t" + stock + "\t\t" );
    System.out.printf("$%.2f\t\t", price);
}

it will compile. Your version of this method will not compile because the variables you refer to (idNumber, gameTitle, unitStock and unitPrice) are the formal parameters of the constructor; you need to use the instance variables (i.e. number, title, stock and price) in your method.

Another solution to your problem might be to rename the instance variables and redefine the constructor as follows:
CODE

//Note, only displays the first part of the Product class
public class Product
{
    public int idNumber; // number in inventory
    public String gameTitle; // name of game in inventory
    public int unitStock; // number of units in stock
    public double unitPrice; // price of unit in inventory

// constructor for Product class
public Product(int idNumber, String gameTitle, int unitStock, double unitPrice)
{
    this.idNumber = idNumber;
    this.gameTitle = gameTitle;
    this.unitStock = unitStock;
    this.unitPrice = unitPrice;
}
...

If you go for this solution, there will be no need to alter the displayInventory() method.

I hope that I didn't miss out anything important and I also hope that all of this was of any value to you.
User is offlineProfile CardPM
+Quote Post

Martyr2
RE: Print Statement Errors
17 Aug, 2007 - 09:59 AM
Post #4

Programming Theoretician
Group Icon

Joined: 18 Apr, 2007
Posts: 5,655



Thanked: 313 times
Expert In: C/C++, Java, VB, VB.NET, C#, PHP, Web Development, HTML & CSS, Javascript

My Contributions
Hi,

You had a few things wrong here but you were not too far off. First of all, printf is fine because System.out is a type of printstream. Your main problems were related to scope of your variables and accessing the right variable names.

CODE

public class Product
{
        // These variables belong to the class Product.

    public int number; // number in inventory
    public String title; // name of game in inventory
    public int stock; // number of units in stock
    public double price; // price of unit in inventory

    // constructor for Product class
    public Product(int idNumber, String gameTitle, int unitStock, double unitPrice)
    {
                // Here we use the passed parameters to set Product's variables
                // Notice how I don't put datatypes on them, I am referencing the ones above with the bigger scope.

        number = idNumber;
        title = gameTitle;
        stock = unitStock;
        price = unitPrice;
    } // end of constructor



    // create objects for inventory
    public static void main( String args[] )
    {
        // create object for first item
        Product myProduct1 = new Product( 01, " Madden ", 3, 39.99);

        myProduct1.displayInventory();

        Product myProduct2 = new Product( 02, " Tiger Woods ", 3, 49.99 );

        myProduct2.displayInventory();

        Product myProduct3 = new Product( 03, " Grand Theft Auto ", 1, 39.99 );

        myProduct3.displayInventory();

        Product myProduct4 = new Product( 04, " Destroy All Humans ", 1, 24.99 );

        myProduct4.displayInventory();

        Product myProduct5 = new Product( 05, " Mafia ", 1, 29.99 );

        myProduct5.displayInventory();

    } // end main


    // method for value anddisplaying members of inventory
    public void displayInventory()
    {
                // Notice here that I am accessing the Product's variables, not the ones passed to product.
                // They were only used to set the products variables which should then be referenced from now on.

        System.out.print( number + "\t" + title + "\t" + stock + "\t\t" );
        System.out.printf("$%.2f\t\t", price);

    } // end display

} // end class Product


The main changes to this code are in Product's constructor as well as the displayInventory() method. Remember that the variable names you gave for the incoming variables are only used to set the product's member variables. All your methods should then access the Product member variable names because they are of higher scope and related to the Product.

This code above has been tested and compiles, the output could use some better organizing, but it is putting out what you designed it to do.

Look at the other member's examples on this board. Right now there are about 3 - 4 other people doing this same project.

Good luck.
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/7/09 06:56PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month