javacompiler-errorsdrjava

multiple classes in Dr. Java


The JDK compiler says it compiles, but when it runs I get this error:

Static Error: This class does not have a static void main method accepting String[].

I am used to putting my methods class in one file and putting the main class in a sperate file.

How do I go about solving this issue?

    import java.util.Scanner;

public class Test{
  private final int classSize = 35;
  private int numEnrolled, numNeeded; 

  public void Input(){
   Scanner in = new Scanner(System.in);
   System.out.println("Enter the amount enrolled in your class");
   numEnrolled = in.nextInt();
   System.out.println("Your input is " + numEnrolled);       

   // Other parts of code that needs to be coded 
  }
}

class testRunner{
  static void main(String args[]){
    Test newTest = new Test();
    newTest.Input();

 }}

Solution

  • Your TestRunner.java should look like this

    class Test{
        private final int classSize = 35;
        private int numEnrolled, numNeeded;
    
        public void Input(){
            Scanner in = new Scanner(System.in);
            System.out.println("Enter the amount enrolled in your class");
            numEnrolled = in.nextInt();
            System.out.println("Your input is " + numEnrolled);
    
            // Other parts of code that needs to be coded
        }
    }
    
    public class TestRunner{
        public static void main(String args[]){
            Test newTest = new Test();
            newTest.Input();
    
        }}
    

    Edit: If you want to leave the file name Test.java, this works too:

    public class Test {
        private final int classSize = 35;
        private int numEnrolled, numNeeded;
    
        public void Input() {
            Scanner in = new Scanner(System.in);
            System.out.println("Enter the amount enrolled in your class");
            numEnrolled = in.nextInt();
            System.out.println("Your input is " + numEnrolled);
    
            // Other parts of code that needs to be coded
        }
    }
    
    class TestRunner {
        public static void main(String args[]) {
            Test newTest = new Test();
            newTest.Input();
    
        }
    }