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

public class Util
{
    public static final String YOUR_TICKET = "your ticket:";
    public static final String NEARBY_TICKETS = "nearby tickets:";

    public static final String OR = "or";

    public static final int CHECKED_NUMBER = -1;

    public static final boolean checkAlCategories (Vector<Category> cats, Ticket t)
    {
        int[] values = new int[t.values().length];

        for (int k = 0; k < values.length; k++)
            values[k] = t.values()[k];

        System.out.println("\nSTART CHECK "+t);

        for (int i = 0; i < cats.size(); i++)
        {
            for (int j = 0; j < values.length; j++)
            {
                if (values[j] != CHECKED_NUMBER)
                {
                    System.out.println("Checking "+values[j]+" against "+cats.elementAt(i));

                    if (cats.elementAt(i).valid(values[j]))
                    {
                        System.out.println("VALID");

                        values[j] = CHECKED_NUMBER;
                    }
                    else
                        System.out.println("INVALID");
                }
            }
        }

        for (int j = 0; j < values.length; j++)
        {
            if (values[j] != CHECKED_NUMBER)
            {
                System.out.println("Number "+values[j]+" is invalid");

                return false;
            }
        }

        return true;
    }

    // read the category data and ignore ticket information.

    public static final Vector<Category> loadCategories (String inputFile, boolean debug)
    {
        /*
         * Open the data file and read it in.
         */

        BufferedReader reader = null;
        Vector<Category> values = new Vector<Category>();

        try
        {
            reader = new BufferedReader(new FileReader(inputFile));

            String line = null;
            boolean finished = false;

            while (((line = reader.readLine()) != null) && !finished)
            {
                if (YOUR_TICKET.equals(line) || (NEARBY_TICKETS.equals(line))) // end
                    finished = true;
                else
                {
                    int colonIndex = line.indexOf(":");

                    if (colonIndex != -1)
                    {
                        String type = line.substring(0, colonIndex);

                        int orIndex = line.indexOf(OR);
                        String firstRange = line.substring(colonIndex +1, orIndex).trim();
                        String secondRange = line.substring(orIndex + OR.length()).trim();
                        int r1 = Integer.parseInt(firstRange.substring(0, firstRange.indexOf("-")));
                        int r2 = Integer.parseInt(firstRange.substring(firstRange.indexOf("-") +1));
                        int r3 = Integer.parseInt(secondRange.substring(0, secondRange.indexOf("-")));
                        int r4 = Integer.parseInt(secondRange.substring(secondRange.indexOf("-") +1));

                        Category cat = new Category(type, r1, r2, r3, r4, debug);

                        if (debug)
                            System.out.println("Loaded "+cat);

                        values.add(cat);
                    }
                }
            }
        }
        catch (Throwable ex)
        {
            ex.printStackTrace();
        }
        finally
        {
            try
            {
                reader.close();
            }
            catch (Throwable ex)
            {
            }
        }

        return values;
    }

    // read the ticket data and ignore categories.

    public static final Vector<Ticket> loadTickets (String inputFile, boolean debug)
    {
        /*
         * Open the data file and read it in.
         */

        BufferedReader reader = null;
        Vector<Ticket> values = new Vector<Ticket>();

        try
        {
            reader = new BufferedReader(new FileReader(inputFile));

            String line = null;
            boolean ticketData = false;

            while ((line = reader.readLine()) != null)
            {
                if (YOUR_TICKET.equals(line) || NEARBY_TICKETS.equals(line))
                {
                    ticketData = true;
                }
                else
                {
                    if (ticketData && (line.indexOf(",") != -1))
                    {
                        String[] range = line.split(",");
                        Ticket t = new Ticket(range);

                        values.add(t);
                    }
                }
            }
        }
        catch (Throwable ex)
        {
            ex.printStackTrace();
        }
        finally
        {
            try
            {
                reader.close();
            }
            catch (Throwable ex)
            {
            }
        }

        return values;
    }

    private Util ()
    {
    }
}
@@ -508,57 +508,8 @@
 %5D;%0A%0A
-        System.out.println(%22%5CnSTART CHECK %22+t);%0A%0A
     
@@ -717,370 +717,98 @@
     
-System.out.println(%22Checking %22+values%5Bj%5D+%22 against %22+cats.elementAt(i));%0A%0A                    if (cats.elementAt(i).valid(values%5Bj%5D))%0A                    %7B%0A                        System.out.println(%22VALID%22);%0A%0A                        values%5Bj%5D = CHECKED_NUMBER;%0A                    %7D%0A                    else%0A                        System.out.println(%22INVALID%22)
+if (cats.elementAt(i).valid(values%5Bj%5D))%0A                        values%5Bj%5D = CHECKED_NUMBER
 ;%0A  
@@ -955,94 +955,8 @@
 ER)%0A
-            %7B%0A                System.out.println(%22Number %22+values%5Bj%5D+%22 is invalid%22);%0A%0A
     
@@ -981,30 +981,16 @@
  false;%0A
-            %7D%0A
         

Update Util.java