Heights HS Computer Programming

This website is maintained by students and alumni. For the official Houston ISD website, please see: houstonisd.org/heights. This website is open source.

Using .dat files in UIL competitions

Why?

When starting the programming portion of a UIL competition, the challenges will more than likely require you to process your input through a .dat file. You may not know how to read a file from the disk, or properly parse its contents. This guide shows you how the UIL graders are expecting you to.

Important note

This guide is assuming you’re using jGrasp and all your Java source files, compiled .class files, and .dat files are in the same directory for simplicity.

What the heck is a .dat file?

The extension .dat is used to denote miscellaneous data. The .dat files used in UIL competitions are plain-text files with sample input data that you can use to test your code. Usually they’re the exact same text that’s on your printed out instructions. These are no different from a .txt file made in notepad.

How do I parse a dat file?

In Java, or any programming language, there are a limitless amount of ways to read data from a file and do what you want with it. What is expected at UIL, though, is to focus on the challenge of deriving an algorithm to solve a bigger problem, instead of being stumped by how you should be reading a file.

The recommended way is to use the two classes: Scanner and File.

These are the same classes you’ve probably used to receive input you’ve typed into your Java programs before. (Here’s a quick refresher)

Example

input.dat

my sample data for input use

ReadingDatFiles.java

//Remember to import the classes
import java.io.File;
import java.io.IOException;
import java.util.Scanner;

public class ReadingDatFiles {

   //Describes that the main method can throw an error
   //If the .dat file isn't found
   public static void main(String[] args) throws IOException {

      //Establishes a link between Java and the file
      File datFile = new File("input.dat");  

      //Creates a Scanner object assigned to the previous file
      Scanner input = new Scanner(datFile);

      //Prints out the input.dat data
      while(input.hasNext()) {
         System.out.println(input.next());
      }

   }
}

Console output

my
sample
data
for
input
use

Real UIL solution using a .dat file

Thanks to: Mike Scott at the University of Texas

burpees.dat

7
100
54
20
365
75
151
29

Burpees.java

import java.io.*;
import java.util.*;
import static java.lang.System.*;

public class Burpees {

	public static void main(String args[]) throws IOException
	{
		Scanner kb = new Scanner(new File("burpees.dat"));

		int times = 0;
		int count = Integer.parseInt(kb.nextLine().trim());
		while(times < count) {
			times++;		

			int num = Integer.parseInt(kb.nextLine().trim());
			int total = num * (num + 1) / 2;
			out.println(total);
		}
	}
}

Console output

5050
1485
210
66795
2850
11476
435

Sources: uiltexas.org, utexas.edu

Other posts
UIL District for March 25
Installing jGrasp on PowerUp Laptops