JAVA Help Thu Sep 28 19:35:45 EDT 2006 API Documentation --- ------------ JAVA API documentation is usually available on-line during a formal contest. The command to access it is: javahelp In a formal contest, you should NOT use other means to access such documentation, as using the internet is a violation of formal contest rules. Program Structure ------- --------- The following is a suitable structure for a program that solves the problem named PPPP: import java.io.*; import java.util.StringTokenizer; public class PPPP { public static boolean debug; public static void dprintln ( String s ) { if ( debug ) System.out.println ( s ); } public static void main (String[] args) throws IOException { debug = ( args.length > 0 ); BufferedReader reader = new BufferedReader ( new InputStreamReader ( System.in ) ); while ( true ) { String line = reader.readLine(); if ( line == null ) break; StringTokenizer tokenizer = new StringTokenizer ( line ); . . . dprintln ( . . . ); System.out.println ( . . . ); } } } Some parts of this structure are described below, and examples are given in demos/count/count1.java and demos/javaio/javaio.java. Debugging --------- In the above program structure, debugging is turned on if the program is called with any arguments (the main function args is of non-zero length). In this case the dprintln function prints, and in the opposite case, where the program is called with no arguments, dprintln does nothing. Input ----- Line oriented input is best done with java.io.BufferedReader.readLine java.util.StringTokenizer.getToken An example is in the count1.java demo. When using this method, strings returned by getToken may have to be converted to numbers. This can be done by code such as String s = ...; int i = Integer.parseInt ( s ); long l = Long.parseLong ( s ); double d = Double.parseDouble ( s ); If line ends are to be treated just like space charac- ters, using java.io.StreamTokenizer is a good idea. An example is in the javaio.java demo. Output ------ Output of floating point numbers with a specified number of decimal places is done with java.text.DecimalFormat See the javaio.java demo. It is important in some contests to use a DecimalFormat object created with the ENGLISH Locale. Numbers that are output should NEVER have commas in them, unless the problem specifically states otherwise. This can be a problem if one uses NumberFormat.get_ instance and then fails to use applyPattern (see the javaio.java demo). String and free format number output examples merely use System.out.println and are given in the count1.java an javaio.java demos. Crashes ------- It is fairly common for JAVA programs which run well for the contestant to crash when run by the judge. The usual cause is judge's input data that triggers an ex- ception not observed with contestant input data. Examples are when an out-of-range array index is used, or the program one runs off the end of an input line and calls getToken with no more tokens. The best debug- ging strategy is to try to find input data that breaks the program. File: java Author: Bob Walton Date: See top of file. The authors have placed this file in the public domain; they make no warranty and accept no liability for this file. RCS Info (may not be true date or author): $Author: walton $ $Date: 2006/09/28 23:37:30 $ $RCSfile: java,v $ $Revision: 1.7 $