wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes Finished my program but have a big problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Finished my program but have a big problem" Watch "Finished my program but have a big problem" New topic
Author

Finished my program but have a big problem

John Vent
Greenhorn

Joined: May 17, 2011
Posts: 11
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
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11499
    
  16

WHO told you there should be a main?

Why can't you call your methods from within a main?


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Greg Brannon
Bartender

Joined: Oct 24, 2010
Posts: 563
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."

Always learning Java, currently using Eclipse on Fedora.
Linux user#: 501795
John Vent
Greenhorn

Joined: May 17, 2011
Posts: 11
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.
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14435
    
  23

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.

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
John Vent
Greenhorn

Joined: May 17, 2011
Posts: 11
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
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3742
    
  16
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 ?


Joanne
John Vent
Greenhorn

Joined: May 17, 2011
Posts: 11
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.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40052
    
  28
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

Joined: May 17, 2011
Posts: 11
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

Joined: Oct 02, 2003
Posts: 11499
    
  16

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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Finished my program but have a big problem