Top
Previous Next
Java's Object Orientation, I/O CS 161 - Java

File Input Example - with Exception handling

Source: examples/InputInterpreter.java



/** InputInterpreter - demonstrate interpreting numeric values from
** input from console (System.in) or a file. Also uses
** overloaded constructors and exception handling.
**/
 
// Classes other than those in java.lang used:
import java.io.IOException;
import java.io.FileReader;
import java.io.BufferedReader;
import java.io.EOFException;
import java.io.FileNotFoundException;
import java.io.InputStreamReader;
 
/** A class that demonstrates reading and interpreting input from
** the console or a file.
**
** @author Jonathan Doughty
**/
public class InputInterpreter {
 
BufferedReader input = null;
boolean consoleInput = false;
 
/** Constructs an object that will read lines for interpretation
** from System.in: the keyboard generally.
**/
public InputInterpreter() {
input = new BufferedReader( new InputStreamReader( System.in ));
consoleInput = true;
}
 
/** Constructs an object that will read lines for interpretation
** from a file with the indicated name.
**/
public InputInterpreter(String fileName)
throws FileNotFoundException
{
input = new BufferedReader( new FileReader( fileName ));
}
 
/** Tell whether input is being read from console.
**/
public boolean isConsole() {
return consoleInput;
}
 
/** Reads a String value from the next line of input,
** trimming blanks from either end, and ignoring blank lines.
**
** @return value interpreted from input
**/
public String readString() throws EOFException {
 
String result = null;
 
try {
while (true) {
// Read a line of from input source
result = input.readLine();
 
// Check if a line was sucessfully read
if (result == null)
throw new EOFException("end of input");
 
// Remove any whitespace from either end
result = result.trim();
 
// Check if the string has zero length
if (result.length() == 0)
continue; // try again
else {
break; // from infinite loop
}
}
}
catch (EOFException eof) {
throw eof; // re-throw it
}
catch (IOException ie) {
// Ignore any other IOExceptions
// (EOFException is a sub-class)
}
return result;
}
 
/** Reads an int value from the next line of input, ignores
** all errors.
**
** @return value interpreted from input
**/
public int readIntValue() throws EOFException {
 
int result = 0;
 
try {
 
String line = readString();
result = Integer.parseInt(line);
}
catch (EOFException eof) {
throw eof; // re-throw it
}
catch (NumberFormatException ne) {
// ignore input that isn't legal
}
return result;
}
 
/** Reads a double value from the next line of input, ignores
** all errors.
**
** @return value interpreted from input
**/
public double readDoubleValue() throws EOFException {
 
double result = Double.NaN; // Initialize to "Not a Number"
 
try {
String line = readString();
Double wrapper = Double.valueOf(line);
result = wrapper.doubleValue();
}
catch (EOFException eof) {
throw eof; // re-throw it
}
catch (NumberFormatException ne) {
// ignore input that isn't legal
}
return result;
}
}


Top
Previous Next
jwd