This question already has answers here:
Error: Main method not found in class Calculate, please define the main method as: public static void main(String[] args) [duplicate]
(5 answers)
Closed 8 years ago.
Error: Main method not found in class MovieDatabase, please define the main method as: public static void main(String[] args) or a JavaFX application class must extend javafx.application.Application
import java.io.FileInputStream;
import java.util.Scanner;
import java.util.Arrays;
public class MovieDatabase {
private int[] analysis;
//creating the contructor
public MovieDatabase(String file){
analysis = new int[2015];
this.load(file);
}
//uses the load(String file) method from downstairs to do all of the work
public void load(String file){
Scanner theScanner = null;
try{
//inputing the into the scanner
theScanner = new Scanner(new FileInputStream(file));
}
catch(Exception ex){
ex.printStackTrace();
}
// as long as the scanner has another line
while(theScanner.hasNextLine())
{
String Line = theScanner.nextLine();
//make an array called split and allocated different elements based on a seperation of ##
String split[] = Line.split("##");
int year = Integer.valueOf(split[1]);
analysis[year] ++;
}
}
//print out the array in the synchronous format
public void print(){
System.out.printf("%1$-30s %2$10s %3$10s %4$10s ", "Year", "Occurances", "", "");
//go through the array
for (int i =0;i < analysis.length ;i++ ) {
if(analysis[i] >0){
for (int j =i;j < analysis.length ;i++ ){
System.out.printf("%1$-30s %2$10s %3$10s %4$10s ", j, analysis[j], "", "");
}
}
}
}
}
How do I fix this error message?
Ive read other similar questions but just say to make the classes public. Mine are public.
main() method in Java is an standard method which is used by JVM to start execution of any Java program. main method is referred as entry point of Java application which is true in case of core java application
You have missed it. Add following main() method
public static void main(String[] args) {
MovieDatabase db = new MovieDatabase("file/path/goes/here");
db.print();
}
In the Java programming language, every application must contain a
main method whose signature is:
public static void main(String[] args)
As the error suggests, if its non FX project, just define something like:
public static void main(String args[]) {
...
}
Or else change you class definition so it extends Application, something like:
public class MovieDatabase extends Application
To invoke any application JVM need main() method, and which should have following access specifiers and modifiers,
public static void main(String args[])
public - It should be accessible to all
static - JVM not able to create instance of your class so method should be static
void - method not returning anything
For each java application main method is entry point, so your application should have atleast one main method to start execution of application.
It's because you have forgot to add the main method. The error clearly says:
please define the main method as: public static void main(String[]
args)
So add the main:
public static void main(String[] args) {
MovieDatabase m = new MovieDatabase("Your File Path");
m.print();
}
Related
I implement an email system I have a main application class which is scanning some input from the keyboard and while input is not "q" ı have to able to add any command from console. The problem is how can i implement a tester class without changing the main application class. (I need to use already prepared commands within the tester class. They wont be entered explicitly, this means that when we test the code the prepared commands entered own its own). How can do this ? Or is it possible ?
I have 2 main method and I'm not allowed to use Junit.
this is the tester class:
public class CalcTester {
public static void main(String[] args) throws FileNotFoundException {
Scanner scanner =new Scanner(new File("C:\\Users\\burak kaya\\IdeaProjects\\Cacl\\src\\calctest.txt"));
String line ="";
for (int i = 0; scanner.hasNext() ; i++) {
line+=scanner.nextLine();
}
String[] arr =line.split(" ");
CalcApplication.main(arr);
}
application class is like that:
public class CalcApplication {
static Stack<Integer> number = new Stack<>();
public static void main(String[] args) {
Scanner scanner =new Scanner(System.in);
String input =scanner.nextLine();
while(!input.equals("q")) {
if (input.equalsIgnoreCase("s")) {
} else if (input.equalsIgnoreCase("p")) {
} else {
}
}
}
}
I delete inside of the application because it was unnecessary.
My question is, the application works with scanner. On the other hand, the tester class has to use predefined commands to test this application class. How can i do that ?
New to java, and I'm currently relying on lots of output messages to trace activity. I'd like a way to do this cleanly, to make 'semi-redundant' code as inconspicuous as possible.
Originally, I was doing this: (Note the System.out.println statements.)
package hamburger;
...
public class Hamburger extends Application {
...
public static void main(String args[]){
System.out.println("---> hamburger.Hamburger.main");
...
String [] drink = {"pepsi"};
NoCoke.onlypepsi(drink);
... ...
public class NoCoke
public static void main(String args[]){
System.out.println("---> hamburger.NoCoke.main");
...
static void justpepsi(String... commandIn){
System.out.println("---> hamburger.NoCoke.justpepsi");
...
try{ to get coke anyway...
Of course, when I moved things around or renamed stuff, all these literals had to be updated, so I started doing things dynamically using these three lines:
public class Hamburger
public static void main(String[] args) {
String nameClass = new Throwable().getStackTrace()[0].getClassName();
String nameMethod = new Throwable().getStackTrace()[0].getMethodName();
System.out.println("---> "+nameClass+"."+nameMethod);
public class NoCoke
public static void main(String args[]){
<AND HERE>
public static void justpepsi(String... commandIn){
<AND HERE>
It works fine:
---> hamburger.Hamburger.main
---> hamburger.NoCoke.main
---> hamburger.NoCoke.justpepsi
But I hate the way it looks, takes up space, etc.
Is there anything like a 'copy-book' in java? - a doc that contains executable code that can be dropped anywhere? Or is there a procedure to define the code as something like 'a String' that could be declared in the constructor, something like this:
StringThing nameMessageRoutine = new (whatever...) StringThing
"String nameClass = new Throwable()...<etc>...
System.out.println('---> '+nameClass+'.'+nameMethod);'"
...that I could just 'import' or 'refer to' like this:
public class Hamburger extends Application {
public static void main(String args[]){
import: nameMessageRoutine; //outside of the package member
public class NoCoke
public static void main(String args[]){
reference: nameMessageRoutine; //defined in the constructor
This method prints information about from where it was called:
static void printCurrent() {
final StackTraceElement ste = new Throwable().getStackTrace()[1];
String methodName = ste.getMethodName();
String className = ste.getClassName();
String lineNum = ""+ste.getLineNumber();
System.out.println(className + ", " + methodName + ", Line " + lineNum);
}
For example:
package randomTest;
public class MainClass {
public static void main(String[] args) {
printCurrent();
}
}
The output is
randomTest.MainClass, main, Line 131
EDIT:
I know this doesn't exactly answer the question, but it does accomplish the final goal of tracing code activity. To answer the question (as per the title), there is no way in pure Java to automatically insert routines into marked places.
Possible noob question but I cant get my method with parameters in one class to call in the other ?
FirstClass
public class Firstclass {
public static void main(String[] args) {
Test1 test = new Test1();
test.Passingvalue();
test.myMethod();
}
}
SecondClass
import java.util.Scanner;
public class Test1 {
public void Passingvalue (){
Scanner Scan = new Scanner(System.in);
System.out.println("File Name ? ");
String txtFile = Scan.next();
}
public void myMethod(String txtFile){
System.out.print("Scan this file" + txtFile);
}
}
You can provide the parameters as a comma separated list in the brackets after the method's name:
public static void main(String[] args) {
Test1 test = new Test1();
test.myMethod("my_file.txt");
}
Don't forget to add a parameter like this :
test.myMethod("txtFile");
declare your string txtfile as a public static variable outside the two methods (at the beginning of class test1) .
public class Firstclass {
public static void main(String[] args) {
Test1 test = new Test1();
test.Passingvalue();
test.myMethod();
}
}
import java.util.Scanner;
public class Test1 {
String txtFile;
public void Passingvalue (){
Scanner Scan = new Scanner(System.in);
System.out.println("File Name ? ");
txtFile = Scan.next();
}
public void myMethod(){
System.out.print("Scan this file" + txtFile);
}
}
I think you have a misconception here:
public void Passingvalue (){
Scanner Scan = new Scanner(System.in);
System.out.println("File Name ? ");
String txtFile = Scan.next(); //method scope only
}
Here the local variable txtFile only exists until the method Passingvalue (check naming conventions btw) is finished, i.e. it has method scope. Thus when calling myMethod(String txtFile) the parameter has the same name but is a different reference in a different scope.
So you'd either have to pass the file name to your method as the others already suggested or change the scope of txtFile, e.g. make it an instance variable:
public class Test1 {
private String txtFile; //the scope of this variable is the instance, i.e. it exists as long as the instance of Test1 exists.
public void Passingvalue (){
Scanner Scan = new Scanner(System.in);
System.out.println("File Name ? ");
txtFile = Scan.next();
}
public void myMethod(){
System.out.print("Scan this file" + txtFile);
}
}
Please note that this is just meant to illustrate the immediate problem. There are other issues, e.g. with the general design, which are not addressed. The purpose of your code seems to be learning anyways, so design is not that big an issue for now.
Just as a hint: I'd probably pass the name from outside the method or pass/read it in a constructor.
when you are calling a parameterize method you should have to pass a parameter to calling method other wise jvm will not understand to whom method you are calling becuase on the basis of parameters we can over load the methods .
so the final answer of your question is
public static void main(String[] args) {
Test1 test = new Test1();
test.myMethod("place your file name here");
}
When I play it in NETBEANS IDE 8.0 it keeps saying there is no main class even though I added the main class already?
Need help can't understand.
PS. If I delete the static in magic() it blocks the magic() in main.
package fibotail;
import java.util.Scanner;
public class Fibotail {
public static int fibo(int control, int currentValue, int previousValue) {
if (control < 2) {
return currentValue;
}
return fibo(control - 1, currentValue + previousValue, currentValue);
}
public static void magic() {
String cCharacter;
do {
System.out.println("Input here: ");
int something = new Scanner(System.in).nextInt();
for (int i = 1; fibo(i, 0, 1) <= something; i++) {
System.out.println(fibo(i, 0, 1));
}
do {
System.out.println("Do you want to try again? ");
cCharacter = new Scanner(System.in).next();
} while (!(cCharacter.equals("y") || cCharacter.equals("Y") || cCharacter.equals("N") || cCharacter.equals("n")));
} while (cCharacter.equals('y') || cCharacter.equals('Y'));
}
public static int main(String args[]) {
magic();
return 0;
}
}
Return type should be void, not int:
public static void main(String args[]) { ... }
The JVM looks for the exact signature of the method.
When you run your project you would get:
Error: Main method must return a value of type void in class MainTest, please
define the main method as:
public static void main(String[] args)
In other languages than java, where main returns int (such as C and C++) the return code of main becomes the exit code of the process, which is often used by command interpreters and other external programs to determine whether the process completed successfully.
But java needs void as the return value. (Java internal architecture)
If you reaaly need to return a value just use the following:
System#exit(int)
To enable your program quit with a specific exit code which can be interpreted by the operating system.
Your main() method must have return type void
public static void main(String[] args){
}
Not int or other.
main() method is the entry point of your program and JVM is looking exact main() method.
You have to change your code a little bit. It should be:
public static void main(String args[])
The return type of main method is void
How to call the main method?
void prompt()
{
System.out.println("Do you want to continue?");
Scanner confirm = new Scanner(System.in);
String con = confirm.nextLine();
if (con == "y")
{
//call the main method once again.
}
}
When I use
main();
It asks for the value of "args" yet I'm not sure what value I should put in it.
The main() method in a java program takes a String array argument.
public static void main(String[] args) {}
If you do not use the variable args inside of main() you could just pass null to it. Otherwise you would need to pass a String array to the method.
However, you should not be calling the main() method from inside your application. The main() method should be used as an entry point into your application, to launch a program, not be used to recursively execute the logic inside that application. If you have functionality needed again you should put it in a separate method.
Signature of main method is: public static void main(String[] args)
The main method accepts a single argument: an array of elements of type String.
public static void main(String[] args)
This array is the mechanism through which the runtime system passes information to your application. For example:
public static void main(String[] args) {
System.out.println("args = " + args);
}
public static void prompt() {
System.out.println("Do you want to continue?");
Scanner confirm = new Scanner(System.in);
String con = confirm.nextLine();
if (con == "y") {
String[] args = {<set string array>};
main(args);
}
}
For more details, look at this Oracle document: The main Method