hi all. trying to compile this program, but returns an error message stating the
"TreeSet<DeliveryHouseDetails> inputLines = ... "
is an
"unreachable statement"
and points to the
"inputLines"
part. Any ideas at all? Thanks for all responses.
import java.util.*;
import java.io.*;
public class StreetOrder
{
public static void main(String [] args) throws IOException
{
try
{
String inputFileName = args[0];
String outputFilename = args[1];
if (args.length != 2)
// Check this before opening files
throw new ArrayIndexOutOfBoundsException
("Two filenames needed: inputFilename outputFilename");
// Read from the input file and print out to an output file.
BufferedReader in
= new BufferedReader(new FileReader(inputFileName));
PrintWriter out
= new PrintWriter(new FileWriter(outputFilename));
// Check that the input file exists.
File inputFile = new File(inputFileName);
if (inputFile.exists() == false);
throw new FileNotFoundException
("Input file " + args[0] + " does not exist.");
// An tree set to store DeliveryHouseDetails objects.
TreeSet<DeliveryHouseDetails> inputLines = new TreeSet<DeliveryHouseDetails>();
String line;
int number = 1;
// For each line added to the tree set, create a new DeliveryHouseDetails
// object for it. As it's a tree set, it automatically orders each entry
// as they are added - according to the order as desribed in the
// DeliveryHouseDetails class.
while ((line = in.readLine()) != null)
{
inputLines.add(new DeliveryHouseDetails(number, line));
number++;
}
// Throw a new exception if the contents of the input file is empty.
if (inputLines == null || inputLines.size() == 0)
throw new Exception("This file has no content");
// Each element is accessed via the iterator.
Iterator<DeliveryHouseDetails> iterator = inputLines.iterator();
// Then writes to the desired output file.
while (iterator.hasNext())
out.println(iterator.next().getPersonName());
// Exit and close all readers and writers.
in.close();
out.close();
} // try
// Catch the exceptions.
catch (ArrayIndexOutOfBoundsException exception)
{
System.out.println("Please supply only 2 arguments, "
+ "inputFilename outputFilename.");
} // catch
catch (FileNotFoundException exception)
{
System.out.println("Input file not found - please check that it exists.");
} // catch
catch (IOException exception)
{
System.out.println("Please supply only 2 arguments, "
+ "inputFilename outputFilename.");
} // catch
catch (Exception exception)
{
System.out.println("This file has no content.");
} // catch
} // main
} // class StreetOrder