1

I am new to java. This is the code i wrote in a text editor and saved as .java file.

public class Prime {

    public static void main(String[] args) {

        int num = 29;
        boolean flag = false;
        for(int i = 2; i <= num/2; ++i)
        {
            // condition for nonprime number
            if(num % i == 0)
            {
                flag = true;
                break;
            }
        }

        if (!flag)
            System.out.println(num + " is a prime number.");
        else
            System.out.println(num + " is not a prime number.");
    }
}

But, when i tried to compile this file through CMD. I got a message that the class is public and it has to be declared in Prime.java. I didn't understand this, so i removed public modifier. And then compiled the code and then it successfully created a CLASS file. What can i do to overcome the above problem?

Stultuske
  • 9,296
  • 1
  • 25
  • 37

1 Answers1

1

Compiler expects the .java file to be named by class name. For example, the file public class Employee{ .., ... } needs to be named as Employee.java

Reddy_73
  • 134
  • 11