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

  • SEARCH
  • Home
  • 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 7437785
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T10:27:08+00:00 2026-05-29T10:27:08+00:00

After developing in PHP for a long time I have decided to step into

  • 0

After developing in PHP for a long time I have decided to step into Java. Comfortable in OOP methodology and all that, I’m trying to start off at that point within java, but I’m getting hung up on passing out my arraylist object into a for statement to be printed back out using the Item class methods.

HelloInvetory.java

package helloInventory;

import java.util.Arrays;

public class HelloInventory {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Object InvetoryItems;

        Inventory inv = new Inventory();
        inv.createItemObj(101, "camera", "Used camera that I bought off of a homeless guy.", 500);
        InvetoryItems = inv.getAllInventoryItems();

        for(Object item : InvetoryItems){
               System.out.println(item.getItemName());
        }

        System.out.println("Done");
    }

}

Inventory.java

package helloInventory;

import java.util.*; 

/**
 * Tracks and maintains all items within the inventory
 * @author levi
 *
 */
public class Inventory {

    List<Object> InventoryItems = new ArrayList<Object>();

    /*
     * create object from Items class
     * and insert into Object[] array.
     */
    public void createItemObj(int sku, String name, String descriptor, float price) {
        Items item = new Items();
        item.setSku(sku);
        item.setItemName(name);
        item.setItemDescription(descriptor);
        item.setItemPrice(price);

        this.setInventoryItems(item);
    }
    public Object getAllInventoryItems() {
        //return InventoryItems;
         return this.InventoryItems.toArray();
    }

    public void setInventoryItems(Object inventoryItems) {
        //InventoryItems.add(inventoryItems);
         this.InventoryItems.add(inventoryItems);
    }
}

Items.java

package helloInventory;
/**
 * Class object to hold each item details
 * @author levi
 *
 */
public class Items {

    int sku;
    String itemName;
    String itemDescription;
    float itemPrice;

    public int getSku() {
        return sku;
    }
    public void setSku(int sku) {
        this.sku = sku;
    }
    public String getItemName() {
        return itemName;
    }
    public void setItemName(String itemName) {
        this.itemName = itemName;
    }
    public String getItemDescription() {
        return itemDescription;
    }
    public void setItemDescription(String itemDescription) {
        this.itemDescription = itemDescription;
    }
    public float getItemPrice() {
        return itemPrice;
    }
    public void setItemPrice(float itemPrice) {
        this.itemPrice = itemPrice;
    }

}

Where I am stuck is within the HelloInventory.java

for(Object item : InvetoryItems){
               System.out.println(item.getItemName());
        }

IDE (Eclipse) gives me the error “Can only iterate over an array or an instance of java.lang.Iterable”. Is there something extra I need, or I’m I going around this totally the wrong way in Java? Correct example would be helpful.

Best,
Levi

  • 1 1 Answer
  • 0 Views
  • 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-29T10:27:09+00:00Added an answer on May 29, 2026 at 10:27 am

    You have a very strange architecture here my friend. You shouldn’t be using generic Objects everywhere, but the actual types. First thing:

    public Object getAllInventoryItems() {
        //return InventoryItems;
         return this.InventoryItems.toArray();
    }
    

    Why not just return the List itself?

    public List<Item> getAllInventoryItems() {
         return this.InventoryItems;
    }
    

    Also change this:

    List<Item> InventoryItems = new ArrayList<Item>();
    

    and this:

    public void setInventoryItems(Item inventoryItems) {
         this.InventoryItems.add(inventoryItems);
    }
    

    Now iterating the List is smooth sailing:

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        List<Item> InvetoryItems;
    
        Inventory inv = new Inventory();
        inv.createItemObj(101, "camera", "Used camera that I bought off of a homeless guy.", 500);
        InvetoryItems = inv.getAllInventoryItems();
    
        for(Item item : InvetoryItems){
               System.out.println(item.getItemName());
        }
    
        System.out.println("Done");
    }
    

    Btw, I changed Items to Item out of habit. A class name should indicate a single entity so by convention it’s singular.

    Now don’t take this the wrong way, but you may have got off on the wrong foot with Java, so I highly recommend this reading: http://www.mindview.net/Books/TIJ/ This worked for me when I was starting with Java, maybe others can suggest some good sources as well.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a PHP script that is inserting a record into a database after
I'm developing a LoB application in Java after a long absence from the platform
i installed joomla,apache server,php,mysql in my pc, after that how to start developing web
After developing a WPF application without Source Control, I decided to add the solution
Our team has just started developing for the Sitecore CMS. We find that after
This is a trick question, while developing a php+ajax application i felt into some
i'm developing a php page that manage the access to some pubs.... right now
I'm developing a PHP-based web-application in which you have a form with textarea inputs
I am developing and doing all the testing on a local machine using PHP
I've run across an interesting PHP/SOAP error that has me stymied. After searching I

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.