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

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




Inventory Program

2 Pages V  1 2 >  
Reply to this topicStart new topic

Inventory Program

mudpuppy
13 Aug, 2007 - 08:19 PM
Post #1

New D.I.C Head
*

Joined: 13 Aug, 2007
Posts: 17


My Contributions
I know that there is already codes for numerous different inventory programs. I have read them, and I am still somewhat confused. OK, this is what my code is supposed to be doing: Modify the Inventory Program by creating a subclass of the product class that uses one additional unique feature of the product you chose (for the DVDs subclass, you could use movie title, for example). In the subclass, create a method to calculate the value of the inventory of a product with the same name as the method previously created for the product class. The subclass method should also add a 5% restocking fee to the value of the inventory of that product.
• Modify the output to display this additional feature you have chosen and the restocking fee.

This is the errors I keep getting: /tmp/27473/Product.java:10: class, interface, or enum expected
{
^
/tmp/27473/Product.java:13: class, interface, or enum expected
product[1] = new Product(2, "Clown Trigger", 1, 74.99);
^
/tmp/27473/Product.java:14: class, interface, or enum expected
product[2] = new Product(3, "Blueline Trigger", 2, 59.99);
^
/tmp/27473/Product.java:15: class, interface, or enum expected
product[3] = new Product(4, "Queen Trigger", 1, 99.99);
^
/tmp/27473/Product.java:18: class, interface, or enum expected
total = (inventory[ 0 ].getValueOfStock() +
^
/tmp/27473/Product.java:23: class, interface, or enum expected
System.out.printf( "The total value of the inventory is: $%.2f\n\n", total );
^
/tmp/27473/Product.java:26: class, interface, or enum expected
public Product(int number, String name, int quantity, double price) {
^
/tmp/27473/Product.java:28: class, interface, or enum expected
this.name = name;
^
/tmp/27473/Product.java:29: class, interface, or enum expected
this.quantity = quantity;
^
/tmp/27473/Product.java:30: class, interface, or enum expected
this.price = price;
^
/tmp/27473/Product.java:31: class, interface, or enum expected
}
^
/tmp/27473/Product.java:33: class, interface, or enum expected
public String getName() { return name; }
^
/tmp/27473/Product.java:33: class, interface, or enum expected
public String getName() { return name; }
^
/tmp/27473/Product.java:34: class, interface, or enum expected
public int getNumber() { return number; }
^
/tmp/27473/Product.java:34: class, interface, or enum expected
public int getNumber() { return number; }
^
/tmp/27473/Product.java:35: class, interface, or enum expected
public double getPrice() { return price; }
^
/tmp/27473/Product.java:35: class, interface, or enum expected
public double getPrice() { return price; }
^
/tmp/27473/Product.java:36: class, interface, or enum expected
public int getQuantity() { return quantity; }
^
/tmp/27473/Product.java:36: class, interface, or enum expected
public int getQuantity() { return quantity; }
^
/tmp/27473/Inventory.java:26: illegal start of expression
public void getInventory() {
^
20 errors

Finally, here is my codes:
CODE
public class Inventory
{
  
   public static final int MAXIMUM_ITEMS = 4;
   Product product[] = new Product[MAXIMUM_ITEMS];

public Inventory(){
          buildInventory();
          getInventory();        
    }

    public void buildInventory() {

    
        product[0] = new Fish(0, "Fancy Tailed Guppy",        25, 4.99);
        product[1] = new Fish(1, "Huma Huma Picasso Trigger", 2, 35.99);
        product[2] = new Fish(2, "Black Durgeon Trigger",        1, 99.99);
        product[3] = new Fish(3, "Niger Trigger",        2, 45.99);
        product[4] = new Fish(4, "Jack Dempsey Cichlid", 4, 19.99);
        product[5] = new Fish(5, "Midas Cichlid",        1, 69.99);
        product[6] = new Fish(6, "Neon Tetra",        25, 2.99);
        product[7] = new Fish(7, "Convict Cichlid",        2, 15.99);
        product[8] = new Fish(8, "Oscar", 4, 37.99);
        product[9] = new Fish(9, "Albino Cory Cat",        1, 29.99);

        public void getInventory() {
        for(int i = 0; i < product.length; i++) {
            System.out.println("Number:     " + product.getNumber());
            System.out.println("Name:       " + product.getName());
            System.out.println("Quantity:   " + product.getQuantity());
            System.out.println("Price:      " + product.getPrice());
            System.out.println("Item Value: " +
                product.getQuantity() * product.getPrice());
            System.out.printIn("Item Value:"+product.getQuantity() * product.getPrice() * 0.05);
            System.out.println();

        
      }  
   }
}


CODE
public class Product {

    private String name = "";
    private int number = 0;
    private double price = 0;
    private int quantity = 0;
}

      
{

        product[0] = new Product(1, "Bursa Trigger", 3, 39.99);
        product[1] = new Product(2, "Clown Trigger",        1, 74.99);
        product[2] = new Product(3, "Blueline Trigger",       2, 59.99);
        product[3] = new Product(4, "Queen Trigger",       1, 99.99);


               total = (inventory[ 0 ].getValueOfStock() +
               inventory[ 1 ].getValueOfStock() +
               inventory[ 2 ].getValueOfStock() +
               inventory[ 3 ].getValueOfStock() +
               inventory[ 4 ].getValueOfStock());
      System.out.printf( "The total value of the inventory is: $%.2f\n\n", total );

    
    public Product(int number, String name, int quantity, double price) {
        this.number = number;
        this.name = name;
        this.quantity = quantity;
        this.price = price;
    }

    public String getName() { return name; }
    public int getNumber() { return number; }
    public double getPrice() { return price; }
    public int getQuantity() { return quantity; }
}



CODE
public class Fish{
    
        public Fish() {}

public static void main( String args[] ) {
         new Inventory ();
    }

}


I have been reading and rereading, but I just get more and more confused. When I think I do something right, I ask my teacher and he makes me feel like an idiot. Any help at all is greatly appreciated. Thanks!

Ok, I've been working on the code trying to get rid of some of the errors. I am down to one in my inventory.java file. This is the error: Inventory.java:26: illegal start of expression
public void getInventory() {
^
This is the errors for the other codes: /tmp/18746/Inventory.java:26: illegal start of expression
public void getInventory() {
^
/tmp/18746/Product.java:9: class, interface, or enum expected
product[0] = new Product(1, "Bursa Trigger", 3, 39.99);
^
/tmp/18746/Product.java:10: class, interface, or enum expected
product[1] = new Product(2, "Clown Trigger", 1, 74.99);
^
/tmp/18746/Product.java:11: class, interface, or enum expected
product[2] = new Product(3, "Blueline Trigger", 2, 59.99);
^
/tmp/18746/Product.java:12: class, interface, or enum expected
product[3] = new Product(4, "Queen Trigger", 1, 99.99);
^
/tmp/18746/Product.java:15: class, interface, or enum expected
(inventory[ 0 ].getValueOfStock() +
^
/tmp/18746/Product.java:20: class, interface, or enum expected
System.out.printf( "The total value of the inventory is: $%.2f\n\n", total );
^
/tmp/18746/Product.java:23: class, interface, or enum expected
public Product(int number, String name, int quantity, double price) {
^
/tmp/18746/Product.java:25: class, interface, or enum expected
this.name = name;
^
/tmp/18746/Product.java:26: class, interface, or enum expected
this.quantity = quantity;
^
/tmp/18746/Product.java:27: class, interface, or enum expected
this.price = price;
^
/tmp/18746/Product.java:28: class, interface, or enum expected
}
^
/tmp/18746/Product.java:30: class, interface, or enum expected
public String getName() { return name; }
^
/tmp/18746/Product.java:30: class, interface, or enum expected
public String getName() { return name; }
^
/tmp/18746/Product.java:31: class, interface, or enum expected
public int getNumber() { return number; }
^
/tmp/18746/Product.java:31: class, interface, or enum expected
public int getNumber() { return number; }
^
/tmp/18746/Product.java:32: class, interface, or enum expected
public double getPrice() { return price; }
^
/tmp/18746/Product.java:32: class, interface, or enum expected
public double getPrice() { return price; }
^
/tmp/18746/Product.java:33: class, interface, or enum expected
public int getQuantity() { return quantity; }
^
/tmp/18746/Product.java:33: class, interface, or enum expected
public int getQuantity() { return quantity; }
^
20 errors

Doesn't look like I've made much difference!

Here is the code once again:

CODE
public class Inventory
{
  
   public static final int MAXIMUM_ITEMS = 4;
   Product product[] = new Fish[MAXIMUM_ITEMS];

public Inventory(){
          buildInventory();
          getInventory();        
    }

    public void buildInventory() {

    
        product[0] = new Fish(0, "Fancy Tailed Guppy",        25, 4.99);
        product[1] = new Fish(1, "Huma Huma Picasso Trigger", 2, 35.99);
        product[2] = new Fish(2, "Black Durgeon Trigger",        1, 99.99);
        product[3] = new Fish(3, "Niger Trigger",        2, 45.99);
        product[4] = new Fish(4, "Jack Dempsey Cichlid", 4, 19.99);
        product[5] = new Fish(5, "Midas Cichlid",        1, 69.99);
        product[6] = new Fish(6, "Neon Tetra",        25, 2.99);
        product[7] = new Fish(7, "Convict Cichlid",        2, 15.99);
        product[8] = new Fish(8, "Oscar", 4, 37.99);
        product[9] = new Fish(9, "Albino Cory Cat",        1, 29.99);

        public void getInventory() {
        for(int i = 0; i < product.length; i++) {
            System.out.println("Number:     " + Fish.getNumber());
            System.out.println("Name:       " + Fish.getName());
            System.out.println("Quantity:   " + Fish.getQuantity());
            System.out.println("Price:      " + Fish.getPrice());
            System.out.println("Item Value: " +
                Fish.getQuantity() * Fish.getPrice());
            System.out.printIn("Item Value:"+product.getQuantity() * product.getPrice() * 0.05);
            System.out.println();

        
      }  
   }
}


public class Product {

    private String name = "";
    private int number = 0;
    private double price = 0;
    private int quantity = 0;
}

        product[0] = new Product(1, "Bursa Trigger", 3, 39.99);
        product[1] = new Product(2, "Clown Trigger",        1, 74.99);
        product[2] = new Product(3, "Blueline Trigger",       2, 59.99);
        product[3] = new Product(4, "Queen Trigger",       1, 99.99);


              (inventory[ 0 ].getValueOfStock() +
               inventory[ 1 ].getValueOfStock() +
               inventory[ 2 ].getValueOfStock() +
               inventory[ 3 ].getValueOfStock() +
               inventory[ 4 ].getValueOfStock());
      System.out.printf( "The total value of the inventory is: $%.2f\n\n", total );

    
    public Product(int number, String name, int quantity, double price) {
        this.number = number;
        this.name = name;
        this.quantity = quantity;
        this.price = price;
    }

    public String getName() { return name; }
    public int getNumber() { return number; }
    public double getPrice() { return price; }
    public int getQuantity() { return quantity; }
}

        
public class Fish{
    
        public Fish() {}

public static void main( String args[] ) {
         new Inventory ();
    }

}


Again, thanks for any help!

This post has been edited by mudpuppy: 13 Aug, 2007 - 09:05 PM
User is offlineProfile CardPM
+Quote Post

Martyr2
RE: Inventory Program
14 Aug, 2007 - 09:36 AM
Post #2

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
Ok, you have quite a bit of things wrong with this. Most of your mistakes come from what appears to be a lack of understanding of how to develop a class, subclass it and use them in a program. So I will go through the parts and provide you some code that will work. You will of course need to do a bit of work yourself using the overriden function I have created for you.

So the first problem is that you haven't defined a good Product class. This class will serve as your base class which you will inherit from (extend). It is crucial that you write a good base class first. A class should only describe itself and provide methods that are relevant only to a Product. Nothing about inventory or Fish should appear in the class. It should only describe what a Product IS and what it can DO.

Product.java

CODE

// Product class, this is a base class which Fish will subclass from.
// You only define basic fields and methods specific to a product item.
// Nothing from inventory or fish should be found here.

public class Product {

    private String name = "";
    private int number = 0;
    private double price = 0;
    private int quantity = 0;

    // Constructor
    public Product(int number, String name, int quantity, double price) {
        this.number = number;
        this.name = name;
        this.quantity = quantity;
        this.price = price;
    }

    // Public accessor methods
    public String getName() { return name; }
    public int getNumber() { return number; }
    public double getPrice() { return price; }
    public int getQuantity() { return quantity; }

    // Get value of product (will be overriden by Fish subclass)
    public double getValue() {
         return (double) quantity * price;
    }
}


This product describes the ID number, its name, its quantity and its price related to a product. Lastly we have given it a method to retrieve its value (quantity * price) = value.

Now that we have a strong base class describing a product, we need to make a "more specific" product called Fish. This is a type of product which is important to realize. A product isn't necessarily a fish, but a fish IS A product. The assignment asked that you inherit from the product (make it a subclass) and then override one of its base methods... its value.

Fish.java

CODE

// This class defines a fish. A fish IS A product so we extend the Product base class.
// Fish is now a subclass of Product (a more specify type of product)

public class Fish extends Product {
    
        public Fish(int number, String fishname, int fishquantity, double fishprice) {
                super(number, fishname, fishquantity, fishprice);
        }

    // Lets override the Product's getValue() function
    // First we call the base class function
    // Then we tack on that 5% fee.
    // This function then returns the value of the product + stocking fee.

    public double getValue() {
        double inventoryvalue = super.getValue();
        return (inventoryvalue * 1.05);
    }    
}


Notice how we define a fish by first passing its parameters to the super class (our product base class) and then we define a method with the same name and return type as our base class. This overrides the base class method. In it we then get the value from our Product super class and tack on the 5% fee. Remember we use 1.05 to increase the value 5%. Using .05 would give us the actual restocking fee (as you will see in the inventory class main program).

One thing I also want to point out is the use of the word "extends" at the top of this class. This will make Fish a "subclass" of product. A fish product EXTENDS the functionality of a Product.

Lastly the inventory main program. Here you have all sorts of problems with curly braces in the wrong spots (this causes a lot of your errors), you lacked proper dimensions on your product array, and you were referencing your products the wrong way while looping.

Inventory.java

CODE

// Inventory Class (Main Program)

public class Inventory
{
  
        // We setup our inventory to hold 10 products
        public static final int MAXIMUM_ITEMS = 10;
        private static Product product[] = new Fish[MAXIMUM_ITEMS];

        public static void main(String args[]) {
                buildInventory();
                getInventory();        
        }

        // Build the inventory, storing 10 different products.
        public static void buildInventory() {

                // Store the more specify fish class into its superclass array.
                // Since a fish "IS A" product, this will work. We couldn't store a Product
                // in an array of fish because a Product is NOT a fish.

                product[0] = new Fish(0, "Fancy Tailed Guppy",25, 4.99);
                product[1] = new Fish(1, "Huma Huma Picasso Trigger",2, 35.99);
                product[2] = new Fish(2, "Black Durgeon Trigger",1, 99.99);
                product[3] = new Fish(3, "Niger Trigger",2, 45.99);
                product[4] = new Fish(4, "Jack Dempsey Cichlid",4, 19.99);
                product[5] = new Fish(5, "Midas Cichlid",1, 69.99);
                product[6] = new Fish(6, "Neon Tetra",25, 2.99);
                product[7] = new Fish(7, "Convict Cichlid",2, 15.99);
                product[8] = new Fish(8, "Oscar", 4, 37.99);
                product[9] = new Fish(9, "Albino Cory Cat",1, 29.99);
        }


        // Loop through the products and get the various details of each.
        public static void getInventory() {
                for(int i = 0; i < product.length; i++) {
                    System.out.println("Number:     " + product[i].getNumber());
                    System.out.println("Name:       " + product[i].getName());
                    System.out.println("Quantity:   " + product[i].getQuantity());
                    System.out.println("Price:      " + product[i].getPrice());
                    System.out.println("Item Value: " + product[i].getQuantity() * product[i].getPrice());
            
                        // This will need changing on your part to get the products value!

                    System.out.println("Restock Fee: " + (product[i].getQuantity() * product[i].getPrice()) * 0.05);
                    System.out.println();

              }  
       }
}


Starting from the top you will notice that we made your product array private and static but more importantly a dimension of 10! Why? Because you were storing 10 items, not 4. Second thing is that we put in main here. main should only appear in your main program file. You don't define it in any class definitions. When you run the program it looks for main and only one main to start the program. You had main in your fish class which was the definition of a product type. Since your inventory uses products and fish, it should be where your program starts.

Your buildinventory function was good, but when you went to loop through them you had one syntax error (one of your println functions was printIn (using an I instead of a L). Lastly when you go to access one of the items, you were using the subclass name, you need to get a reference to the actual product element in the array. So you reference it using the index variable on the array. There I have left it up to you to get the value of the product.

Last thing I wanted you to notice is how we were able to define fish and store it in a product array. Since a fish is a type of product we can store the fish in the product array just fine. We could not have stored a product in a fish array since a product isn't necessarily a fish. When we go to reference it in the loop however, it will call the right overriden function based on the type, which it will identify as a fish. If it wasn't a fish, it would call the regular base class version of getValue().

Hope this helps and learn all you can from this example. It is crucial to you in going further with programming.

Enjoy!
User is offlineProfile CardPM
+Quote Post

mudpuppy
RE: Inventory Program
14 Aug, 2007 - 11:35 AM
Post #3

New D.I.C Head
*

Joined: 13 Aug, 2007
Posts: 17


My Contributions
Thank you sooooo very much, Martyr2! You have made it possible for me to understand this a little better! You are a God send! When I submitted my codes to my teacher and a different forum, I was treated like an idiot. You, on the other hand, did not do this and explained everything so well just so I could understand what it was I was doing wrong! Again, thank you so very much! Now I have to figure out how to add a GUI to this program. There are three more parts to this program that I have to do over the course of the next two weeks, so I am sure I will be back to this forum very soon!
User is offlineProfile CardPM
+Quote Post

skyhawk133
RE: Inventory Program
14 Aug, 2007 - 11:42 AM
Post #4

Head DIC Head
Group Icon

Joined: 17 Mar, 2001
Posts: 15,262



Thanked: 61 times
Dream Kudos: 1650
Expert In: Web Development

My Contributions
QUOTE
When I submitted my codes to my teacher and a different forum, I was treated like an idiot. You, on the other hand, did not do this and explained everything so well just so I could understand what it was I was doing wrong!


Tell your friends smile.gif

Thanks Martyr2 for helping out! As usual, you rock.
User is online!Profile CardPM
+Quote Post

Martyr2
RE: Inventory Program
14 Aug, 2007 - 11:52 AM
Post #5

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
I am glad I could be of assistance mudpuppy. I, and the rest of the Dream.In.Code team, try to strive to be better than other boards when it comes to prompt and precise answering. I am sure that next time you visit you may not get me answering the question quickly, but you will certainly get another qualified person who won't treat you like dirt. Programming is meant to be fun! So if you know anyone else looking for help, give us a shout and I am sure they will enjoy the site too. Heck, maybe they can even provide an answer or two to someone else as a way to say thanks! smile.gif
User is offlineProfile CardPM
+Quote Post

mudpuppy
RE: Inventory Program
14 Aug, 2007 - 12:36 PM
Post #6

New D.I.C Head
*

Joined: 13 Aug, 2007
Posts: 17


My Contributions
I've already emailed everyone in my class to tell them about this forum! By far, this one is the best one out there! biggrin.gif

Quick question. This is what I haev to add to the code this week: Modify the Inventory Program to use a GUI. The GUI should display the information one product at a time, including the item number, the name of the product, the number of units in stock, the price of each unit, and the value of the inventory of that product. In addition, the GUI should display the value of the entire inventory, the additional attribute, and the restocking fee

Should this go into a .java file all by itself and be called by the inventory or fish.java file or should the GUI just go into the inventory.java file? In the reading it gives an example of a GUI to make a rainbow. (Still not real sure how to make this GUI incorporate it into the program as not being a rainbow.) I am working on that though! lol

This post has been edited by mudpuppy: 14 Aug, 2007 - 12:41 PM
User is offlineProfile CardPM
+Quote Post

mudpuppy
RE: Inventory Program
14 Aug, 2007 - 07:33 PM
Post #7

New D.I.C Head
*

Joined: 13 Aug, 2007
Posts: 17


My Contributions
QUOTE(mudpuppy @ 14 Aug, 2007 - 01:36 PM) *

I've already emailed everyone in my class to tell them about this forum! By far, this one is the best one out there! biggrin.gif

Quick question. This is what I haev to add to the code this week: Modify the Inventory Program to use a GUI. The GUI should display the information one product at a time, including the item number, the name of the product, the number of units in stock, the price of each unit, and the value of the inventory of that product. In addition, the GUI should display the value of the entire inventory, the additional attribute, and the restocking fee

Should this go into a .java file all by itself and be called by the inventory or fish.java file or should the GUI just go into the inventory.java file? In the reading it gives an example of a GUI to make a rainbow. (Still not real sure how to make this GUI incorporate it into the program as not being a rainbow.) I am working on that though! lol



Ok I submitted my code to my teacher. This is what he said: Your output is something like $14.329202, change the format to dollars and cents only two digits behind the decimal. So I made a change and resubmitted it. He said I made everything worse! This is what he said: Go back to what you had before and slowly make changes, I just executed and this became worse:

Number: 0
Name: Fancy Tailed Guppy
Quantity: 25
Price: 4.99
Item Value: 124.75
Restock Fee: 6.237500000000001

Exception in thread "main" java.util.MissingFormatArgumentException: Format specifier 's'
at java.util.Formatter.format(Unknown Source)
at java.io.PrintStream.format(Unknown Source)
at java.io.PrintStream.printf(Unknown Source)
at Inventory.getInventory(Inventory.java:46)
at Inventory.main(Inventory.java:13)
Again, here is my code :
CODE



public class Inventory
{
  
        
        public static final int MAXIMUM_ITEMS = 10;
        private static Product product[] = new Fish[MAXIMUM_ITEMS];

        public static void main(String args[]) {
                buildInventory();
                getInventory();        
        }

        
        public static void buildInventory() {

                

                product[0] = new Fish(0, "Fancy Tailed Guppy",25, 4.99);
                product[1] = new Fish(1, "Huma Huma Picasso Trigger",2, 35.99);
                product[2] = new Fish(2, "Black Durgeon Trigger",1, 99.99);
                product[3] = new Fish(3, "Niger Trigger",2, 45.99);
                product[4] = new Fish(4, "Jack Dempsey Cichlid",4, 19.99);
                product[5] = new Fish(5, "Midas Cichlid",1, 69.99);
                product[6] = new Fish(6, "Neon Tetra",25, 2.99);
                product[7] = new Fish(7, "Convict Cichlid",2, 15.99);
                product[8] = new Fish(8, "Oscar", 4, 37.99);
                product[9] = new Fish(9, "Albino Cory Cat",1, 29.99);
        }


        
        public static void getInventory() {
                for(int i = 0; i < product.length; i++) {
                    System.out.println("Number:     " + product[i].getNumber());
                    System.out.println("Name:       " + product[i].getName());
                    System.out.println("Quantity:   " + product[i].getQuantity());
                    System.out.println("Price:      " + product[i].getPrice());
                    System.out.println("Item Value: " + product[i].getQuantity() * product[i].getPrice());
            
                        

                    System.out.println("Restock Fee: " + (product[i].getQuantity() * product[i].getPrice()) * 0.05);
                    System.out.printf("\n%s's Inventory is $%.2f\n");
                    System.out.println();

              }  
       }
}



I feel about ready to bang my head against my computer desk!
User is offlineProfile CardPM
+Quote Post

Martyr2
RE: Inventory Program
14 Aug, 2007 - 08:50 PM
Post #8

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
Lookup the printf function's use. You are missing the second part of that function, where you list the variables you want placed into the first part's string.

CODE

String hello ="Hi there!";

// This prints out "I went to my friend and said Hi there!"
System.out.printf("I went to my friend and said %s",hello);


You are right about using the %.2f to format that dollar amount though. Be sure to test before submitting to your teacher so that you know it works as it should.
User is offlineProfile CardPM
+Quote Post

mudpuppy
RE: Inventory Program
15 Aug, 2007 - 03:37 AM
Post #9

New D.I.C Head
*

Joined: 13 Aug, 2007
Posts: 17


My Contributions
QUOTE(Martyr2 @ 14 Aug, 2007 - 09:50 PM) *

Lookup the printf function's use. You are missing the second part of that function, where you list the variables you want placed into the first part's string.


I looked the printf function's up and I was forgetting to the the . in front of \n So it should have read $%.2f.\n Is this right? I think some of the errors my teacher keeps getting is because of minor errors on my part that produce major errors.

How do I test my code? All I go by is if it compiles or not. I have tried to run the .class files that the compiler produces, but my computer won't allow me to run them. Is there something I am missing?

Thanks!
User is offlineProfile CardPM
+Quote Post

Martyr2
RE: Inventory Program
15 Aug, 2007 - 09:44 AM
Post #10

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
You can simply run your code by opening up your DOS command prompt, navigating to the directory where your file is, compile it using the javac command and then run it using java filenamewithnoextension.

Example...

CODE

c:\myjavaproject>javac Inventory.java

c:\myjavaproject>java Inventory


Notice how I didn't specify an extension and used the "java" command to run it. As for your formatter question, you just need to use %.2f\n if you want to format it to two decimal places (for money) and get a newline afterwards.

Hope this helps.

User is offlineProfile CardPM
+Quote Post

mudpuppy
RE: Inventory Program
15 Aug, 2007 - 11:58 AM
Post #11

New D.I.C Head
*

Joined: 13 Aug, 2007
Posts: 17


My Contributions
QUOTE(Martyr2 @ 15 Aug, 2007 - 10:44 AM) *

As for your formatter question, you just need to use %.2f\n if you want to format it to two decimal places (for money) and get a newline afterwards.

Hope this helps.


That is how I originally turned it in to my teacher. He said that was wrong. He said that it's giving me a readout of something like $12.34567890 (Just using those numbers as an example) He does not make sense to me though because that is the same printf function I used for my Employee Payroll Program a couple weeks ago. That one worked just fine without that long number and gave a correct readout as far as I know. If it was wrong then he didn't say anything about it. I agree with you though Martyr2, I know it is to use %.2f\n. I even went back and checked my code from the previous assignments.

This post has been edited by mudpuppy: 15 Aug, 2007 - 12:02 PM
User is offlineProfile CardPM
+Quote Post

mudpuppy
RE: Inventory Program
15 Aug, 2007 - 08:53 PM
Post #12

New D.I.C Head
*

Joined: 13 Aug, 2007
Posts: 17


My Contributions