Most CompSciComp problems come with a .dat file that contains inputs that need to be processed in accordance with the problem specifications. The name of the dat file will correspond with the name of the class you are expected to write. The structure and content of the dat file will be specified in the problem itself.
Consider a problem that asks you to write a class called Example and provides you with an example.dat file. Your solution should read the content of the dat file using the Scanner class and process that data in some way. The example below shows how to use a while loop to iterate over the dat file content, but sometimes a for loop will be more appropriate.
The dat file will be assumed to be located at root of the classpath. Normally, that means the .dat file, .java file, and .class file should be in the same folder. However, if you are using a professional level IDE, your dat files, java files, and class files may need to be located in different folders.
Your class should NOT use the package statement. A class that identifies as part of a package will not compile on the judge machine and will be marked incorrect.
If you are using a professional level IDE, double check that it does not automatically add a package statement to the top of your java file.
Additionally, if any non-standard packages are imported into your class (even if you don't use any classes from the package in your code), it will not compile and will be judged incorrect. If you are using a professional level IDE,
double check that it did not attempt to import a non-standard package into your code.
Your solution should always output its results to the console using System.out.print and System.out.println. Only the final result should be output. Any additional debugging information or data output to the System.err stream will cause the solution to be judged incorrect.
import java.util.*; import java.io.*; public class Example { public static void main(String[] args) throws Exception { // Open up the .dat file Scanner s = new Scanner(new File("example.dat")); // Iterate over each string token in the .dat file while(s.hasNext()) { // Read the next string token from the .dat file String str = s.next(); // TODO: process str String answer = str + " "; // Output your answer System.out.print(answer); } } }
Hello World!