• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Java help.

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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:

And:

Here is the error:


If I remove void I then get an error that I am leaving out the return info.
 
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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...
 
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"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.
 
Robert Nino
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Alright so I'm going to remove void how will I right the return statement?
 
Sunil Kumar
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Marshal
Posts: 79153
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
     
    Bartender
    Posts: 2911
    150
    Google Web Toolkit Eclipse IDE Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    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 !!

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

     
    We cannot change unless we survive, but we will not survive unless we change. Evolving tiny ad:
    a bit of art, as a gift, that will fit in a stocking
    https://gardener-gift.com
    reply
      Bookmark Topic Watch Topic
    • New Topic