-1

Possible Duplicate:
Exception in thread “main” Java.lang.NoSuchMethodError: main ??

public class InsertionSort
{
public static void main ( int[] a)
{
    int j;
    for( int p=1 ; p<a.length ; p++)
    {
        int tmp = a[p];
        for( j=p ; j>0 && tmp<a[j-1] ; j--)
        {
            a[j] = a[j-1];
        }
        a[j] = tmp;
    }
}
}

And this happens in Terminal. (I'm on a Mac if it matters) javac InsertionSort.java;java InsertionSort Exception in thread "main" java.lang.NoSuchMethodError: main

Community
  • 1
  • 1
Helgi
  • 11

4 Answers4

3

You need a proper main() to make the class runnable. A main method should have an array of string as the only argument, you have an array of ints.

So, to solve it, redeclare it to "public static void main(String[] args)" and do the integer parsing in the method. Neither java nor the OS will do that conversion for you.

Fredrik
  • 5,759
  • 2
  • 26
  • 32
1
public static void main (String[] arg)

main accepts array of string, not array of int.

x.509
  • 2,205
  • 10
  • 44
  • 58
1

The JVM lookt for a public static void main(String[]) signature, not for a main method that takes an int[] as argument.

rsp
  • 23,135
  • 6
  • 55
  • 69
1

It will run if you do it like this:

public static void main ( String[] args)
{
    int[] a = new int[args.length];
    for(int i = 0; i < args.length; i++){
        a[i]=Integer.parseInt(args[i]);
    }
    int j;
    for( int p=1 ; p<a.length ; p++)
    {
        int tmp = a[p];
        for( j=p ; j>0 && tmp<a[j-1] ; j--)
        {
            a[j] = a[j-1];
        }
        a[j] = tmp;
    }
}

A main method, needs a String array, you need an int array, so we'll just convert the one to the other.

Sean Patrick Floyd
  • 292,901
  • 67
  • 465
  • 588