• Post Reply Bookmark Topic Watch Topic
  • New Topic

No main method found, main method is present  RSS feed

 
Greenhorn
Posts: 14
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

Just a quick question, I have a program that compiles but won't run and tells me "No main methods found in file" when I have a main method with "public static void main"
Here is the line with main method


I've probably done something dumb, so thank you in advance for any help.
 
Ranch Hand
Posts: 76
3
IntelliJ IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The main method is specified not only by the "public static void main" part, but also by the parameter list. It has to take only one argument, and this has to be an array of Strings (or String vararg). Look at your method. Does it fulfill this requirement?
 
Kiera Hakikawa
Greenhorn
Posts: 14
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I know it does not. I added those parameters to use methods I have in another class because I don't know another way to do so.
Seeing as I can't do it this way, what is a good way to use methods of another class in the main method?
 
Marshal
Posts: 56608
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You don't. You use the main method to start your application and nothing else. Look at our FAQ.
 
Bartender
Posts: 3271
82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How does adding parameters allow you to use methods in another class?

If you need to pass values to your class when you start it you specify them on the command line and they get put into the single String array parameter ie
java MyClass arg1 arg2 arg3

would result in the main method of MyClass being called with a String array 3 elements long containing "arg1", "arg2" and "arg3". If any of the parameters you want to pass are of a type other than String (eg int) then you need to convert them from a String to the appropriate type in the main method.
 
Kiera Hakikawa
Greenhorn
Posts: 14
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In my main method I need to use methods in another class.
I cannot add them to the String array because this assignment deals with linked list.
However I have fixed this issue with
this in the main


and these in the other class
 
Sheriff
Posts: 4935
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I know nothing about your application, but I see something from what you showed.
1. Why do you think method from standard API is named toUpperCase()?
Answer: because its name reveals its intension.

Question: why did you wrap around clear method with unclear your method, which is named altName()?
Ok, lets assume this name makes sense and it converts name to an upper cases.

Question, what the method altHeight() suppose to do?
My height is 188 (cm). How it would look 188 in upper cases? Do you see my point? Same with altDiameter(). Or these methods do something different from altName()? - Of course they do, so why the heck you chose such poor names for your methods. Try to revise them and make them clear what they are meant to do.

Brainstorming:
altName > nameToUpperCase
altHeigh > don't know what it does, but could be for instance heightToCentimetres
altDiameter > ... work out yourself

And this:
could be changed to:
Beside that, I don't see the point here to wrap it to another method, while name.toUpperCase is clear.
 
Campbell Ritchie
Marshal
Posts: 56608
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is actually another error; the method returns the upper case equivalent of the name field in the other class, but it also has the side‑effect of changing that name field to upper case.
 
Kiera Hakikawa
Greenhorn
Posts: 14
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Changed to
 
Sheriff
Posts: 22846
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kiera Hakikawa wrote:In my main method I need to use methods in another class.


If they are static methods then you just call methods of the other class directly. Example:



To call that method:



And if they are instance methods then you create an instance of the other class and call methods of that instance. Example:



To call that method:



 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!