Win a copy of The Journey To Enterprise Agility this week in the Agile and Other Processes forum! And see the welcome thread for 20% off.
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Junilu Lacar
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Ganesh Patekar
  • Tim Moores
  • Pete Letkeman
  • Stephan van Hulst
Bartenders:
  • Carey Brown
  • Tim Holloway
  • Joe Ess

Finished my program but have a big problem  RSS feed

 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So I have finally completed my program.

All Junit tests complete, however I have just been informed there should be a main.

If I try to create a main I cannot call any of my methods from within this.

I just want a main that has something like chose option x
when you type in a specific option it in turn runs the appropriate method.

A lot of my methods have this. which flags as a problem.

Is there any obvious way to get around this?

Thanks
 
lowercase baba
Bartender
Posts: 12627
50
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
WHO told you there should be a main?

Why can't you call your methods from within a main?
 
Bartender
Posts: 563
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you're getting an error message, please post the exact message.

Also, explain the statement: "If I try to create a main I cannot call any of my methods from within this."
 
John Vent
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My Professor informed me I should have a main else how will a user start the program.

My methods just will not work within main. In my primary class I have added a main method which I would like to run this classes and other classes methods.

If I for example have the code

if (selection == 1){
print()

In eclipse this show up as being incorrect with the message "Cannot make a static reference to the non-static method print() from the type InventMain"

If I change the print method to static as it suggests I then get an error here that I cannot use this. :
for (final IDetails input : this.items) {
System.out.println(input.toString() );

I'm at a loss I really thought I had this finished, help urgently and great fully appreciated.
 
Java Cowboy
Sheriff
Posts: 16084
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The main() method is a static method. That means that, unlike non-static method, it is not called on an object - it's a method that belongs to the class, rather than to a specific instance of the class. In order to call a non-static method from a static method, you have to call the non-static method on some object. For example:

For more details, see: Understanding Instance and Class Members.
 
John Vent
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I see what you mean, the problem I am now having is that I want a user to manually enter an items details.
when unit testing this worked fine as I could specifically enter a record and use my add method for each unique item.

Now I can't do this, I need to have input boxes that gives each item a unique id and then enter this into my arrayList.. I can't figure this out
 
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

John Vent wrote:My Professor informed me I should have a main else how will a user start the program.


So how did you start the program ?
 
John Vent
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I didn't as I previously stated I was running JUnit tests... this is all we have ever done so far.
Now I have been told that it should also run as a program with a main.
 
Marshal
Posts: 59742
188
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Very peculiar that you know about JUnit before you knew about the main method. Details about it in the Java™ Language Specification and in this recent thread and the Java™ tutorials.

If you can't understand those three resources, ask again. The JLS is by no means easy to read, but the tutorial should be easier.
 
John Vent
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the advice.

I think I now have this sorted out, I can now add items to array.

One last problem (hopefully)

In my main I now have a menu e.g press 3 to enter item details, user then types values into the console.
I can however only get it to add 1 item as when unit testing I had to give each record a new name and then invent.add.

I can do this for one record but need a way incriment the name with a plus one appended to the end and to the the invent.add method.

My code looks like this

Item one = new Item(barcode, description, quantity, price, day, month, year, name, phone, email);
InventorySystem inventory = new InventorySystem (item1);

addItemToInventory(item1) ;

Each time the user adds a new item (by selecting the appropriate option) I want it to change to
InventorySystem inventory = new InventorySystem (item2);

addItemToInventory(item2) ;

and so on, thanks for all the help, If I can get this done and improve my remove method I think I'm all set.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12627
50
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you don't need to change variable names like that. variable names are for people reading source code, not for the JVM.

I am assuming you are trying to add objects to a collection. you don't need a unique name for the reference each time you add it - in fact, you can re-use the one you have. It's much simpler. it would be something like



Each time through the loop, you create a new Item object. You assign myItem as a reference to it. Once you add it to your Inventory collection, you can re-use the reference and have it point to a new Item. Since your collection has a reference to the original object(s), they do not get lost.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!