0

I am learning Java on OS X through terminal. When I compile the code below I do not get any errors but When I try to run it I get

Exception in thread "main" java.lang.NoSuchMethodError: main

My code:

public class Problem5{
public void main(String[] args) {
    int n = 1;
    while(!checkMod(n)){
        n++;
    }

}
public boolean checkMod(int in)
{
    int count = 0;
    for(int i=1; i<20; i++)
    {
        if(in%i == 0)
        {
            count = count + 1; 
        }
    }
    if(count ==19)
    {
        return true;
    }
    else{
        return false;
    }
}
}

I removed "static" from the main method because I am calling the checkMod method.

How do I code this correctly?

Thanks

stackErr
  • 3,968
  • 3
  • 21
  • 46

7 Answers7

5

Every executable Java program requires at least one class that implements public static void main(String args[]) in order to run.

Rather then removing the static reference from main, either

Add static to the public boolean checkMod(int in) ie public static boolean checkMod(int in)

or

Call the method checkMod from the instance level.

public class Problem5{
    public void main(String[] args) {
        new Problem5();
    }

    public Problem5() {
        int n = 1;
        while(!checkMod(n)){
            n++;
        }

    }

    // Rest of code...
}
MadProgrammer
  • 323,026
  • 21
  • 204
  • 329
  • thanks for the info! This really helps. When would you want to create an instance vs having several static methods? – stackErr Aug 16 '12 at 06:05
  • 1
    `static` references are really good for utilities methods, things that don't require a reference to any instance objects, methods or variables. You want to use instances when you want more then one, well, instance of the object, such as `Person`. You program would be pretty useless if the `Person` class only used `static` references, you'd only ever be able to have one `Person`, BUT, you could use a `static` method to calculate the age, based a `Person`'s date of birth – MadProgrammer Aug 16 '12 at 06:10
1

main need to be static: public static void main(String[] args)

1

The "entry-point" method is static main instead of main. They have different method signature. JVM will only search for static main. You can make your

public boolean checkMod(int in)

as

static public boolean checkMod(int in)
user1192878
  • 684
  • 1
  • 7
  • 19
1

static is required for the main method. You can use something like this to add state to your main execution:

public class Problem5 {
  public static void main(String[] args) {
    new Problem5().start();
  }

  private void start() {
    int n = 1;
    while(!checkMod(n)){
      n++;
    }
  }

  private boolean checkMod(int in) {
    ..
  }
}

On the other hand, your checkMod(int) method doesn't need state. You could also just add the static keyword to it as well. It can then be called by the static main method.

riha
  • 2,190
  • 1
  • 22
  • 39
0

You need to declare 'static' on both main and checkMod.

Gary S.
  • 394
  • 2
  • 7
0

You need to make checkMod static as well, because you're calling it from another static method. You cannot call an instance method from a static method:

public class Problem5{
public static void main(String[] args) {
    int n = 1;
    while(!checkMod(n)){
        n++;
    }

}
public static boolean checkMod(int in)
{
    int count = 0;
    for(int i=1; i<20; i++)
    {
        if(in%i == 0)
        {
            count = count + 1; 
        }
    }
    if(count ==19)
    {
        return true;
    }
    else{
        return false;
    }
}
}
Strelok
  • 46,161
  • 8
  • 92
  • 112
0

Main method should always be declared as static. Also you are accessing checkMod in main method so either declare checkMod also as static or access it with object as you can not access non-static method from static method.

Try following.

public class Problem5{
public static void main(String[] args) {
    int n = 1;
    while(!new Problem5().checkMod(n)){
        n++;
    }

}
public boolean checkMod(int in)
{
    int count = 0;
    for(int i=1; i<20; i++)
    {
        if(in%i == 0)
        {
            count = count + 1;
        }
    }
    if(count ==19)
    {
        return true;
    }
    else{
        return false;
    }
}
}
Sandeep Kumar
  • 12,284
  • 19
  • 66
  • 100