Compilation Error (Java)

This being my first online problem submission ever, I’m not surprised something broke. After submitting Approximately(March13 Cook-off problem), I received a compilation error both when the code was copied/pasted the code into the editor and when uploaded. The error returned was:

Main.java:10: class Approximately is public, should be declared in a file named Approximately.java public class Approximately { ^ 1 error

If it makes a difference, I’m programming in Java. Any suggestions are greatly appreciated!

Seeing this does not relate to the actual solving of the problem, I think it’s perfectly fine to help you with this. However, just to point it out, it is forbidden to post solutions to problems or questions asking for help on problems during a running contest. Additionally, this is the March 13 Challenge, not Cook-Off. The Cook-Off is the short contest that will be held later on this month.

Now to answer your question. Change your class name to Main. When you submit your code in Java, CodeChef takes your code and puts it in its own java file named Main.java. The Java programming language does not allow file names to have names that are different from the name of the public class in the file. Since your class name is “Approximately” and CodeChef’s file name is “Main”, it throws an exception. If you were programming for fun, you would have the option of either changing the class name to the file name or the file name to the class name. But, since you cannot change CodeChef’s file name, you have to change your class name to Main.

So in short, just change your class name to “Main”(quotes for clarity) and you should be fine.

2 Likes

Thank you Kullalok, I didn’t even realize how close I was to disqualifying my self. My noob is really showing. This greatly helped!

Haha, not a problem @gtozzi :). And you’re not a noob; a lot of Java programmers have had this problem as well, myself included. Glad I could help though :).

There is second possibility, your class CANNOT be public, but you can use

class SomeClass {
    // ...
}

notice that public visibility modifier is missing (default aka package visible visibility is used) :wink:

Thank you @betlista :). I never actually thought of that.