TEST: Getting ce

Hi,

I used Eclipse to write the program Life, Universe, Everything. It compiles and works perfectly. However, when I submit, I get the message

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

My file is called LifeUniverseEverything.java

Here’s my code, its more complex than necessary.


import java.io.*;
import java.util.*;

public class LifeUniverseEverything {

	//Private Variables
	private BufferedReader breader;
	
	//Constructor
	public LifeUniverseEverything() {
		breader = new BufferedReader(new InputStreamReader(System.in));
	}
	
	//Pre: User will only input numbers of one or two digits
	//Post: Returns an array consisting of all Strings the user inputs 
	public ArrayList<String> getInput() throws Exception{
		
		 String s = null;
		 ArrayList<String> input = new ArrayList<String>();
		 try {
			     while((s=breader.readLine()) != null) {
			    	 input.add(s);
			     }
			 
		 } catch (Exception E) {
			 System.out.println(E.getMessage());
			 System.exit(1);
	     } 

	     return input;
		
	}
	
	//Pre: result is an ArrayList of Strings. Result is not null
	//Post: Prints each string in result array. Stops when 42 is reached. 
	public void print(ArrayList<String> result) {
		for (int i=0; i<result.size(); i++) {
			if(result.get(i).equals(new String("42"))) {
				break;
			}
			System.out.println(result.get(i));
		}
	}
	
	public static void main(String[] args) throws Exception{
		LifeUniverseEverything start = new LifeUniverseEverything();
		ArrayList<String> result = start.getInput();
		start.print(result);
		
	}
	
}

Okay so i changed the class name to main… and changed every LifeUniverseEverything to Main… and it worked… but I dont see why the former didn’t work =S

The name of the class in Java that contains main function must be “Main”.

Read the FAQ http://www.codechef.com/wiki/faq#Why_do_I_get_a_compile_error

The reason is ,

while compiling ,the java compiler looks first for the main/entry class(out of several classes in your file).After converting the source code into bytecode ,JVM will load the Main class and it will invoke static main method on it.

So ,we have to say the javac ,the Class name of our main class ,and for this reason the file name of the java code file and the main class must be same.

While you submit your code to the code-chef judge ,the judge compiles your solution /code as Main.java file ,and if the javac does not find the class “Main” , the outcome is “compilation error.”

2 Likes

Information from FAQ is not correct, your class do not need to be called Main, see this submission - http://www.codechef.com/viewsolution/812461

I do not know exactly what is the process behind, but there are 2 options:

  1. you name your class Main as @ritesh_gupta described or
  2. you can make your class not public (but use default visibility)

I prefer second option, because I do not want to have all classes called Main (there are problems with compilation - you cannot have two classes with same name in project in Eclipse).

So your class will be

class LifeUniverseEverything {
    ....
}

It works same way on other contest pages, for example SPOJ.

1 Like

Thank you guys!

I lean toward second choice, since I would prefer not to have all classes called Main (there are issues with arrangement - you can’t have two classes with same name in venture in Eclipse) brochure.