File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes Java help. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Java help." Watch "Java help." New topic

Java help.

Robert Nino

Joined: May 03, 2009
Posts: 3
I am having trouble with my Java assignment. I have not been having problems until now. Here is my assignment:
Modify the Inventory Program so the application can handle multiple items. Use an array to store the items. The output 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 output should display the value of the entire inventory. � Create a method to calculate the value of the entire inventory. � Create another method to sort the array items by the name of the product. And here is my current code:


Here is the error:

If I remove void I then get an error that I am leaving out the return info.
David Newton

Joined: Sep 29, 2008
Posts: 12617

One way to approach this is to dissect the line in question and see if it's possible to figure out what the message means.

The line in question is this:The error message is this:What in that line is of "void" type? When we use the "+" operator to perform string concatenation what types of things can we concatenate with a string? What does your Inventory class actually do--create a string or just print one out?
Robert Nino

Joined: May 03, 2009
Posts: 3
The second piece of code is supposed to create it and the first code is supposed to display it. They are 2 different files that are supposed to me linked together...
Sunil Kumar
Ranch Hand

Joined: Apr 24, 2007
Posts: 76
"Current inventory is: " + myInventory.inventory()
To concatenate both the values should be string [or have toString i.e. convertible to string] . myInventory.inventory() is of return type void => which is the Root cause of error, you cannot add "void" to anything. Moreover, its a compile time error.
I dont know what did you want to achieve by appending a method returning nothing, it is anyways useless. If you just want to execute the method, do it in the next line. The question is "Why appending to a string?"

Also if you remove void, you will obviously need to specify some return type to the method and then return something of that type from the method.

Sunil Kumar
Robert Nino

Joined: May 03, 2009
Posts: 3
Alright so I'm going to remove void how will I right the return statement?
Sunil Kumar
Ranch Hand

Joined: Apr 24, 2007
Posts: 76
You should use return statement only if you need it. Dont do it just for the sake of doing it. I mean why dont you call the method in the next line?
Campbell Ritchie

Joined: Oct 13, 2005
Posts: 46349
I think what you are doing is mistaking a method for a constructor or vice versa. You have a method called public void inventory, which does three things. That is not a good idea; a method should do one thing.
It does these three things
  • Set up the contents of the inventory.
  • Calculate the total value of each line.
  • Print the details.

  • You ought to have the setting-up done in a constructor, but not the other two things. A constructor can be identified by
  • Having a name exactly the same as the name of the class (including CaSe Of LeTtErS).
  • Having no return type.

  • You should work out the inventory values when you request them. It would appear that you need a getInventoryValues method which works out all the inventory values and returns them as a double[]. Yes, you return a reference to an array.
    Then you should organise a print() method, preferably in conjunction with a toString() method. Like thisAnd good luck and welcome to JavaRanch

    And please don't write "Java help" as a thread title.
    salvin francis
    Ranch Hand

    Joined: Jan 12, 2009
    Posts: 1118

    The Compiler is asking you:

    What should i append at the end of "Current inventory is: " ???
    the function you are writing there is not returning a value !!

    solution1: (mentioned by Sunil Kumar)
    just call myInventory.inventory();
    you dont need a System.out.println(); at all !!

    let function return a value (usually string containing the message).

    I agree. Here's the better link:

    I agree. Here's the link:
    subject: Java help.
    It's not a secret anymore!