Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 340247
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T10:35:54+00:00 2026-05-12T10:35:54+00:00

Question: Where does productArray come from in these: for ( Product product : productArray

  • 0

Question: Where does productArray come from in these:

for ( Product product : productArray )

&

Arrays.sort( productArray, new ProductComparator() );

Question: What am I doing wrong? How do I make this sort?

Related Post from Yesterday


                                 EDIT

:::EDIT:::
Ok I took your advice here about

Product productArray[] = new Product[ARRAY_LENGTH]

Now it is broke here

/tmp/jc_22339/InventoryPart2.java:99: cannot find symbol
symbol  : variable productArray
location: class InventoryPart2
    for ( Product product : productArray ) {
                            ^
/tmp/jc_22339/InventoryPart2.java:101: cannot find symbol
symbol  : variable productArray
location: class InventoryPart2
        inventoryValue += productArray.calcInventoryValue();
                          ^

And if I do it like

for ( Product product : productArray[] ) {

I get

/tmp/jc_23524/InventoryPart2.java:69: '.class' expected
    for ( Product product : productArray[] ) {
                                           ^

So I am back stuck.


Begin program

:::Updated Code:::

    /**
 This program stores a collection of a product and its variables in a java array
 It will sort and display the information with a total
 */

// Import statements go here
import java.util.Scanner;// Import and use scanner
import java.util.Arrays;


public class InventoryPart2 {

    /**
     * @param args the command line arguments
     */

    public static void main(String[] args) {// begin main

        // Define your array of product objects and index here
        final int ARRAY_LENGTH = 5;// declare constant 
        final int version = 2;// declare int version number   

        // Create instance of Scanner class
        Scanner input = new Scanner( System.in );// new Scanner for CL input
        // Set counter to loop x times to populate your array of product objects
        int counter = 0;

        // Initialize your product array with the number of objects to populate it with
        Product productArray[] = new Product[ARRAY_LENGTH];// create array Product of class Product 

        // Welcome message
        System.out.printf( "\n%s%d\n" , 
        "Welcome to the Productentory Program v.", version );

        // Construct default values for Product
        productArray[0] = new Product("EThe Invisible Man", 0, 8.50);
        productArray[1] = new Product("DThe Matrix", 1, 17.99);
        productArray[2] = new Product("CSe7en", 7, 12.99);
        productArray[3] = new Product("BOceans Eleven", 11, 9.99);
        productArray[4] = new Product("AHitch Hikers Guide to the Galaxy", 42, 18.69);

        /*// Loop as many times as your counter variable
        {

            // Instantiate a product object

            // Prompt for product name and call the set method on your product object

            // Prompt for item number and call the set method on your product object

            // Prompt for units in stock and call the set method on your product object

            // Prompt for unit price and call the set method on your product object

            // store product object in array element

            //Destroy product object reference
            product = null;

            // Flush the buffer
            input.nextLine();

        }*/

        // Sort product array by product name using Comparator implementation class
        sortProductArray();

        // Print sorted array
        for ( Product product : productArray[] ) {
        if ( counter == 0 )
            System.out.printf( "\n%s\n", "Sorted Inventory of DVD movies");
        System.out.printf( "\n\n%s%d\n%s%s\n%s%d\n%s%,.2f\n%s%,.2f\n",
        "Item Number:      ",counter,
        "DVD Title:        ",productArray[counter].getProductTitle(),
        "Copies in stock:  ",productArray[counter].getUnitsInStock(),
        "Price each disk:  $",productArray[counter].getUnitPrice(),
        "Value of disks:  $",productArray[counter].calcInventoryValue());//End print
        counter++;  
        if ( counter == productArray.length)// on last counter of loop print total
            System.out.printf( "\n%s%,.2f\n\n\n",
            "Collection Value: $",calcTotalInventoryValue());
        }

        // Calculate total Inventory value

    }


 // method to calculate the total Inventory value
 private static double calcTotalInventoryValue()
 {

    double inventoryValue = 0;

    // Iterate array of product objects and calculate total value of entire Inventory
    for ( Product product : productArray ) {
        // accumulate inventory value from each product object in array
        inventoryValue += productArray.calcInventoryValue();
    }

     return totalInventoryValue;

 } // end method calcInventoryValue


 // method to sort product array
 private static void sortProductArray()
 {

    Arrays.sort( productArray, new ProductComparator() );



 } // end method calcInventoryValue


}
  • 1 1 Answer
  • 1 View
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T10:35:54+00:00Added an answer on May 12, 2026 at 10:35 am

    You either have to have an array of Product as a member of the InventoryPart2 class OR passed into the sortProductArray method. You’ll have to change that method to return a reference to the sorted array if you choose the latter.

    Your Product array is in the static main method and not part of the object, so you’ve got a problem. I’d recommend that you do something like this:

    public class Inventory
    {
        private static final int DEFAULT_INVENTORY_SIZE = 5;
        private Product [] products;
        private int numProducts;
    
        public Inventory()
        {
           this(DEFAULT_INVENTORY_SIZE);
        }
    
        public Inventory(int size)
        {
           products = new Product[size];
           numProducts = 0;
        }
    
        public void addProduct(Product p)
        {
           products[numProducts++] = p;
    
        }
    
        public void sort()
        {
            // sort the array here
        }
    
        public String toString()
        {
            StringBuilder builder = new StringBuilder(1024);
    
            // Create a string representation of you inventory here
    
            return builder.toString();
        } 
    }
    

    Object oriented programming is about encapsulation and information hiding. Write the class in such a way that clients don’t have to know or care whether you’re using an array or something else to hold onto Products. You’re abstracting the idea of Inventory here.

    UPDATE:

    This is your code, only better and running:

    import java.util.Scanner;
    import java.util.Arrays;
    
    public class InventoryPart2
    {
       public static void main(String[] args)
       {
          final int ARRAY_LENGTH = 5;
          final int version = 2;
    
          int counter = 0;
    
          Product productArray[] = new Product[ARRAY_LENGTH];
    
          System.out.printf("\n%s%d\n",
          "Welcome to the Productentory Program v.", version);
    
          productArray[0] = new Product("EThe Invisible Man", 0, 8.50);
          productArray[1] = new Product("DThe Matrix", 1, 17.99);
          productArray[2] = new Product("CSe7en", 7, 12.99);
          productArray[3] = new Product("BOceans Eleven", 11, 9.99);
          productArray[4] = new Product("AHitch Hikers Guide to the Galaxy", 42, 18.69);
    
          productArray = sortProductArray(productArray);
    
          // Print sorted array
          for (Product product : productArray)
          {
             if (counter == 0)
             {
                System.out.printf("\n%s\n", "Sorted Inventory of DVD movies");
             }
             System.out.printf("\n\n%s%d\n%s%s\n%s%d\n%s%.2f\n%s%.2f\n",
             "Item Number:      ", counter,
             "DVD Title:        ", product.getProductTitle(),
             "Copies in stock:  ", product.getUnitsInStock(),
             "Price each disk:  $", product.getUnitPrice(),
             "Value of disks:  $", product.calcInventoryValue());
             counter++;
          }
          System.out.printf("\n%s%,.2f\n\n\n",
          "Collection Value: $", calcTotalInventoryValue(productArray));
       }
    
       private static double calcTotalInventoryValue(Product[] productArray)
       {
          double inventoryValue = 0;
    
          for (Product product : productArray)
          {
             inventoryValue += product.calcInventoryValue();
          }
    
          return inventoryValue;
    
       }
    
       private static Product[] sortProductArray(Product[] productArray)
       {
          Arrays.sort(productArray, new ProductComparator());
    
          return productArray;
       }
    }
    

    I removed those comments you add everywhere. They’re just clutter; I’d recommend that you not do that anymore. Better to make your code more readable and self-documenting by using better variable and method names.

    This still isn’t the way I’d recommend that you write it, but it works. You’re more likely to see why this works if I don’t alter it too much.

    UPDATE 2:

    Just in case you’re interested, here’s how I might write it:

        import java.util.Scanner;
    import java.util.Arrays;
    import java.text.NumberFormat;
    
    public class Inventory
    {
       private static final int DEFAULT_LENGTH = 5;
       private static final int VERSION = 2;
    
       private Product[] products;
       private int numProducts;
    
       public static void main(String[] args)
       {
          Inventory inventory = new Inventory(5);
    
          inventory.addProduct(new Product("EThe Invisible Man", 0, 8.50));
          inventory.addProduct(new Product("DThe Matrix", 1, 17.99));
          inventory.addProduct(new Product("CSe7en", 7, 12.99));
          inventory.addProduct(new Product("BOceans Eleven", 11, 9.99));
          inventory.addProduct(new Product("AHitch Hikers Guide to the Galaxy", 42, 18.69));
    
          System.out.println(inventory);
          System.out.println("total value: " + NumberFormat.getCurrencyInstance().format(inventory.getTotalValue()));
       }
    
       public Inventory()
       {
          this(DEFAULT_LENGTH);
       }
    
       public Inventory(int size)
       {
          products = new Product[size];
    
          this.numProducts = 0;
       }
    
       public void addProduct(Product p)
       {
          products[numProducts++] = p;
       }
    
       public double getTotalValue()
       {
          double inventoryValue = 0.0;
    
          for (Product product : products)
          {
             inventoryValue += product.calcInventoryValue();
          }
    
          return inventoryValue;
       }
    
       public String toString()
       {
          StringBuilder builder = new StringBuilder(1024);
          String newline = System.getProperty("line.separator");
    
          if (products.length > 0)
          {
             Arrays.sort(products, new ProductComparator());
             for (Product product : products)
             {
                builder.append(product).append(newline);
             }
          }
    
          return builder.toString();
       }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 171k
  • Answers 171k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Are you using Eclipse Webtools? If you're not, consider doing… May 12, 2026 at 2:16 pm
  • Editorial Team
    Editorial Team added an answer From the Apple documentation (Core Data programming guide): Note that… May 12, 2026 at 2:16 pm
  • Editorial Team
    Editorial Team added an answer Make a copy of the image and crop it to… May 12, 2026 at 2:16 pm

Related Questions

Where does Rails store data created by saving activerecord objects during tests? I thought
Where does the object MSXML2.ServerXMLHTTP.4.0 come from? Which install package? I'm attempting to do
Where does ViEmu save my custom mappings and settings I use ? Something like
Where does Firefox store cookies and in what format are they stored

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.