Example Java Programs



General Information

Some Common, Simple Tasks

How to get some simple chores done: random numbers, date formatting, properties, string tokenizer, stream tokenizer, and enumeration types.

Random Numbers

Date Formatting

(We looked at number formatting earlier.) An internationalized resource bundling using the Java class and class.

The class uses special pattern characters to refer to the components of a date or time text.

SymbolMeaningPresentation KindExample
Gera designatorTextAD
yyearYear yyy:96 yyyy:1996
Mmonth in yearMonth M:7 MM:07 MMM:Jul MMMM:July
wweek in yearNumber27
Wweek in monthNumber2
Dday in yearNumber189
dday in monthNumber10
F day of week in month;
as in: 2nd Tue in Jul
Number2
Eday in weekAbbreviated Text EEE:Tue EEEE:Tuesday
aam/pm markerTextPM
Hhour in day (0-23)Number13
khour in day (1-24)Number24
Khour in am/pm (0-11)Number0
hhour in am/pm (1-12)Number11
mminute in hourNumber m:3 mm:03 mmm:003
ssecond in minuteNumber55
SmillisecondNumber978
ztime zoneAbbreviated Text zzz:EST zzzz:Eastern Standard Time
Ztime zone (RFC 822)Text-0800

Properties

Property Namedescription
file.encoding The character encoding for the default locale
file.encoding.pkgThe package that contains the converters
that handle converting between local encodings and Unicode
java.awt.fonts
java.awt.graphicsenv
java.awt.printerjob
java.class.path The value of the CLASSPATH environment variable
java.class.version The version of the Java API
java.compiler The just-in-time compiler to use, if any.
Initialized from the environment variable JAVA_COMPILER.
java.ext.dirs The directory in which Java extentions are installed
java.home The directory in which Java is installed
java.io.tmpdir The directory in which Java should create temporary files
java.security.policy
java.version The version of the Java interpreter
java.vendor A vendor-specific string
java.vendor.urlA vendor URL
java.vm.info
java.vm.name
java.vm.specification.name
java.vm.specification.vendor
java.vm.specification.version
java.vm.vendor
java.vm.version
file.separatorThe platform-dependent file separator (e.g., "/" on UNIX, "\" for Windows)
path.separator The platform-dependent path separator (e.g., ":" on UNIX, "," for Windows)
line.separator The platform-dependent line separator (e.g., "\n" on UNIX, "\r\n" for Windows)
os.arch The system architecture
os.name The name of the operating system
os.version The operating system version
user.dir The current working directory when the properties were initialized
user.home The home directory of the current user
user.languageThe two-letter language code of the default locale
Solaris: initialized from env var LANG
user.name The username of the current user
user.region The two-letter country code of the default locale
user.timezoneThe default time zone
font.propertiesJava 1.2 AWT
swing.defaultlafControl look-and-feel
awt.image.incrementaldrawif true (default) draw parts of images
as it arrives.
awt.image.redrawrateMinimum period in milliseconds
between calls to repraint (default is 100)
propertyUnixWindowsMacIntosh
file.separator/\:
line.separator\n\r\n\r
path.separator:;:
readln reads all three types of lines on all three types of systems. println writes the kind of line appropriate for the system. line+line.separator File(String) recognizes only the file separator for the system it is being run on.

Property File Format

A description of the file format for property files can be found in the documentation for the method of the class

String Tokenizer

The following are examples using the Java class. They are often used with (see examples elsewhere). The Java class is often used while reading the input as in the Read.java program.

Stream Tokenizer

Examples using the Java class.

A stream tokenizer is initialized to the following default state:

Unless the syntax table is reset, the StreamTokenizer methods add new ranges of characters to the individual categories and does not replace the initial ranges.

Regular Expressions

Using method

Enumeration Types

No enumeration type like C++'s
      enum color { red, orange, yellow, green, blue };
      

Ryan Stansifer <ryan@cs.fit.edu>
Last modified: Tue Nov 4 16:24:06 EST 2003