1

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;
    }
}
takendarkk
  • 3,347
  • 8
  • 25
  • 37
  • http://stackoverflow.com/questions/540396/why-is-main-in-java-void – nobalG Aug 01 '14 at 05:49
  • it should be public static void main(String args[]) and not public static int main(String args[]). – Darshan Lila Aug 01 '14 at 05:49
  • `public static int main (String args[])` != `public static void main (String[] args)`. Fix it :) – FoggyDay Aug 01 '14 at 05:59
  • "_I added the main class already_" You have a method named `main` but it doesn't have the correct signature (yours has the wrong return type) which should have been `public static void main(String{} args)`. – takendarkk Aug 01 '14 at 06:41

4 Answers4

2

Return type should be void, not int:

public static void main(String args[]) { ... }

The JVM looks for the exact signature of the method.

morgano
  • 17,210
  • 10
  • 45
  • 56
1

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.

Ruchira Gayan Ranaweera
  • 34,993
  • 17
  • 75
  • 115
1

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.

0

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

tomrozb
  • 25,773
  • 31
  • 101
  • 122
Sanjay Dutt
  • 2,192
  • 16
  • 16