1

Possible Duplicate:
Causes of 'java.lang.NoSuchMethodError: main Exception in thread “main”'

I'm hoping this is just a simple error here, I've looked up numerous other instantces of people getting the same error message but none of their solutions really seem to apply for me. I was just wondering if you guys could help me find the error in my code. I'm not even sure if it's functional because I can't get it to run, so I suppose it could be a logic error.

When I try to run the following cold I am met with fatal exception error occured. Program will exit.

Eclipse also gives me: java.lang.NoSuchMethodError: main Exception in thread "main"

Thank you very much for any assistance you can offer!

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;



public class JoPuzzle
{

public static Integer main(String[] args)
{
    Scanner input = new Scanner(System.in);

    System.out.println("Please enter the number of soliders");
    int soldiers = input.nextInt();
    System.out.println("Please enter the how many soldiers are skipped before the next death");
    int count = input.nextInt();

     List<Integer> soldiersList = new ArrayList<Integer>(soldiers);
      for (int i = 1; i <= count; i++) {

    soldiersList.add(i);

  }



  int currentIndex = 0;

  while(soldiersList.size() > 1) {

      currentIndex = (currentIndex - 1 + count) % soldiersList.size();

      soldiersList.remove(currentIndex);

  }

  return soldiersList.get(0);

  }   //end main

}//end class
Community
  • 1
  • 1
Sh0gun
  • 711
  • 4
  • 9
  • 16
  • When the program runs, the JVM will check for a method main with signature public static void, which is not present in yours. How is it even starting the execution? – Anuj Balan Mar 12 '12 at 05:14

6 Answers6

2

The signature for main method is
public static void main(String[] args).

When you run your program the JVM will look for this method to execute. You need to have this method in your code

Ashwini Raman
  • 726
  • 1
  • 6
  • 11
  • Oh wow, yes I changed it initially by trying to get rid of the error on my return soldiersList.get(0) instead of simply printing it. Thanks that should have been obvious. I need sleep -_- – Sh0gun Mar 12 '12 at 05:17
2

We know that to execute any java Program we should have a main function. because this is a self callable by JVM. And the signature of the function must be..

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

but in your code it's seem like this...

public static Integer main(String[] args){
 }

so its consider as a different function , so change your main return type..

Sumit Singh
  • 23,530
  • 8
  • 71
  • 99
0

Your program does not contain the main method, change it to

public static void main(String[] args)

If you want to return an Integer object, then define a custom mehod and call that method inside the main method and handle that returned value.

Chandra Sekhar
  • 16,511
  • 14
  • 71
  • 109
  • Or you can exit with a status code to define if the program completed successfully (usually a status of 0) or an error occurred with any other integer you like with System.exit(0); – Dan675 Mar 12 '12 at 05:28
0

Java main() function doesn't have a return-statement. This line

public static Integer main(String[] args)

should be

public static void main(String [] args)

Also since it has no return value, you should delete the return statement.

user1263270
  • 51
  • 1
  • 8
0

Java's main method should have below signature.

public static void main(String[] args){
..
..
}
Balaswamy Vaddeman
  • 7,634
  • 3
  • 28
  • 40
0

Try to run this code and tell if it works.

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;


public class JoPuzzle
{

public static void main(String[] args)
{
    Scanner input = new Scanner(System.in);

    System.out.println("Please enter the number of soliders");
    int soldiers = input.nextInt();
    System.out.println("Please enter the how many soldiers are skipped before the next death");
    int count = input.nextInt();

     List<Integer> soldiersList = new ArrayList<Integer>(soldiers);
      for (int i = 1; i <= count; i++) {

    soldiersList.add(i);

  }



  int currentIndex = 0;

  while(soldiersList.size() > 1) {

      currentIndex = (currentIndex - 1 + count) % soldiersList.size();

      soldiersList.remove(currentIndex);

  }

 // return soldiersList.get(0);

  System.out.println(soldiersList.get(0));
  }   //end main

}//end class
Java
  • 2,371
  • 8
  • 44
  • 82