Java Helper Programs
- JavaDemo.java, Reads and writes to files. (by
Jeff Klein)
, Large print for overhead
What this program does:
- Reads from a file line by line. Each line is read in as a String
- Uses the classes:
BufferedReader and FileReader,
BufferedWriter and FileWriter
- A "try-catch" block is used to catch an IOException errors that may be
thrown by an error in the file opening.
- JavaDemo2.java (thanks to Kedar Hirve for as
sistance)
, Sample text file for JavaDemo2.java
,
Large print for overhead
What this program does:
- Reads from a file line by line. Each line is read in as a String.
- Uses a "try-catch" block to catch IOException's
- Imports class Vector and StringTokenizer from the package java.util
- Stores a Vector of objects of type Integer (this is different from int)
- StringTokenizer is used to collect a set of integers within a String.
- Defines three helper functions (or methods):
getNumValue() returns the int value from a String
getIntVector() collects a String of Integers into a Vector of Integers
printVector() prints the Integer values within a Vector
- Uses the String method String.startsWith() to parse a String into a command
and a number.
- JavaDemo2 Version 2 with separately compi
led classes
What this program does:
- Same as JavaDemo2.java, except uses two separately compiled classes:
class StringProcessor contains the method getNumValue()
class VectorProcessor contains the two metods: getIntArray() and printArray()
- These separate classes are compiled within the same directory
- Reading Strings from the keyboard,
Large print version for overhead.
What this program does:
- Demonstrates keyboard input. echo prints the user's keyboard input
- Stops when the user types a "STOP" command
- Uses BufferedReader and InputStreamReader classes
- Shows keyboard input using System.in as the standard input file
- Java Array Examples
, Large print for overhead
What this program does:
- Shows examples of using a character array: char charArray[]
- Shows examples of using a String
- Printing the decimal, binary, and hexadecimal representations of a character
- Getting the length of an array: charArray.length (No parenthesis!)
- Getting the length of a String: string.length() (Parenthesis!)
- Java "Global" variables with static
, Large print for overhead
What this program does:
- Shows how "static" variables and functions defined outside of main() can be
accessed directly
- Non-static variables can be accessed after you have created an instance
of the class that you are inside
- Java Binary operators
, Large print for overhead
What this program does:
- Demostrates the use of the binary operators AND (&), OR (|), and NOT (~)
- Printing a Character Array in Binary and Hexad
ecimal
, Large print for overhead
What this program does:
- Prints the elements of a character array in binary and hexadecimal form
- Converting from character to int
- Compiling and Running JavaDemo.java
, Large print for overhead
- Java String API and
Java StringBuffer API