Home > database >  Main.java file must have public class with name 'Main'
Main.java file must have public class with name 'Main'

Time:08-13

I'm working on a Java program that will compute for the discount. But I get an error in compiling:

Main.java file must have public class with name 'Main'
You must declared Main class which contains 'main' method, which is entry point of program execution.

Here is the code:

package java_conditional; 
import java.util.Scanner; 
public class Main
{
    public static void main (String[]args) { 
            Scanner scan = new Scanner(System.in); 
            System.out.print("Enter Tution fee: "); 
            int tuition = scan.nextInt(); 
             
            System.out.print("  Scholar Type:\n\n1 Scholar1 80%\n2 Scholar2 50%\n3 Scholar1 25%\nEnter Scholar: "); 
            int scholar = scan.nextInt(); 
             
            switch(scholar) {               case 1: 
                    int scholar1 = (80*tuition)/100; 
                    System.out.println("Discounted (" scholar1 ") 80% of " tuition); 
                    System.out.println("the tuition fee you just have to pay is only " (tuition-scholar1)); 
                    break; 
             
                case 2: 
                    int scholar2 = (50*tuition)/100; 
                    System.out.println("Discounted (" scholar2 ") 50% of " tuition); 
                    System.out.println("the tuition fee you just have to pay is only " (tuition-scholar2)); 
                    break; 
                 
                case 3: 
                    int scholar3 = (25*tuition)/100; 
                    System.out.println("Discounted (" scholar3 ") 25% of " tuition); 
                    System.out.println("the tuition fee you just have to pay is only " (tuition-scholar3)); 
                    break; 
                 
                default: 
                    System.out.println("Not grantee "); 
            }
    }
}

By the way, I'm using Online Java Compiler 'OnlineGDB'.

CodePudding user response:

  1. Remove package java_conditional;
  2. Check if this is the only file you are compiling.
  3. Rename the class name to something more generic, avoid calling classes after methods, this works but causes unnecessary confusion

CodePudding user response:

Just remove package java_conditional; above.

  •  Tags:  
  • java
  • Related