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 8446559
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T09:58:02+00:00 2026-06-10T09:58:02+00:00

I have asked this question before and followed the feedback as best as I

  • 0

I have asked this question before and followed the feedback as best as I could but I am still having one problem with storing the info that the user enters into the array.

Here is the first attempt:
OOP Java: Creating a stock inventory program

So I need to have in total three classes(That’s required). The Stock, stock inventory and then the user interface. The purpose of this program is to ask the user to input the company’s name, stock rating, price and the number of shares. Of course, I then have to do other things. I think I am okay with the rest, the problem is the stockInterface, the last bit of code that I post below.

public class Stock {

private String companyName;
private String stockRating;
private int price;
private int numberOfShares;

public String getCompanyName() {
    return companyName;
}

public int getStockRating() {
    return stockRating;
}

public String getPrice() {
    return price;
}

public int getNumberOfShares() {
    return numberOfShares;
}

public Stock(String companyName, String stockRating, int price, int numberOfShares) {
    super();
    this.companyName = companyName;
    this.stockRating = stockRating;
    this.price = price;
    this.numberOfShares = numberOfShares;
}

import java.util.*;

public class StockInvetory {

private static final int INVENTORY_SIZE = 12;
private Stock [] stocks;

public StockInvetory() {
    stocks = new Stock [INVENTORY_SIZE];

}


public class StockInterface() {
    private static StockInventory stockPortfolio;

        public static void main (String [] args){

    System.out.println ("Stock's name:");
    String stockName = console.next();

    System.out.println ("Stock's rating");
    String stockRating= console.next();

    System.out.println ("Stock's price:");
    int stockPrice = console.nextInt();

    System.out.println ("Numbers of shares: ");
    int numberShares= console.nextInt();

          stockPortfolio [0]= new Stock(stockName, stockRatings, stockPrice, numberShares);
    }

This piece of code doesn’t work.

stockPortfolio [0]= new Stock(stockName, stockRatings, stockPrice, numberShares)

Can somebody please show me the proper way to store the info into the array? Thank you very much.

  • 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-06-10T09:58:04+00:00Added an answer on June 10, 2026 at 9:58 am

    So you’ve declared the stockPortfolio as an instance of StockInventory. StockInventory is a class not an array, so you can’t use stockPortfolio [0] = … because stockPortfolio is an instance of the class. You have a private member in StockInventory that is an array of Stock class instances. What you need is an accessor method to be able to manipulate it. So change StockInventory as follows:

    public class StockInvetory {
    /*
      All the code you have now ...
    */
      public Stock [] getStocks(){
          return stocks;
      }
    
      public setStocks(Stock [] value){
         //maybe some checking here ...
          stocks = value;
      }
    }
    

    Now just a slight change in using the class. You need to use the accessor methods as follows:

    public class StockInterface {
     /*
     What you have just the following line changes ...
     */
       stockPortfolio.getStocks()[0] = new Stock(stockName, stockRatings, stockPrice, numberShares);
    }
    

    I am assuming you are happy with the way you are initializing the array and that you have decided arrays are better than more dynamic data structures in collections for your specific project. If this is not true have a look at Java Collections they may bring you more joy.

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

Sidebar

Related Questions

No doubt elements of this question have been asked before, but I'm having trouble
I have asked this type of a question before but this one IS different.
I have asked this question before but did not get the satisfied answer as
I have asked this question before and the problem was half solved in the
I have asked this before but I didn't get the question right so the
This question may have been asked before, but I had trouble finding an answer,
I know variants of this question have been asked before (even by me), but
I have asked this question before, but have not received any real answer. How
I have asked this question before but I haven't got an answer, so I'm
I have asked this question before - but I have spent some time thinking

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.