Programming and Problem Solving with Java

(やまだぃちぅ) #1

CASE STUDY


464


{

static voidprocessOneSite(BufferedReader inFile, PrintWriter outFile, String
dataSetName)
{
int count; // Loop control variable
doubleamount; // Rainfall amount for one month
doublesum = 0.0; // Sum of amounts for the year
String dataLine; // String to input amount from inFile
String currentValue; // Floating-point string
int index; // Position of blank

try
{
// Next line could produce an IOException
dataLine = inFile.readLine();
for(count = 1 ; count <= 12 ; count++) // For 12 months
{
index = dataLine.indexOf(' '); // Find position of blank
if(index > 0 )
{ // Blank found
currentValue = dataLine.substring( 0 , index); // Extract a number
// Remove current value from string
dataLine = dataLine.substring(Math.min(index+ 1 ,
dataLine.length()), dataLine.length());
}
else // Remaining string is current value
currentValue = dataLine;
// Next line could produce NumberFormatException
amount = Double.parseDouble(currentValue); // Convert to double
if(amount < 0.0)
throw newDataSetException("Negative value in site ");
else
sum = sum + amount;
}
outFile.println("Average for "+ dataSetName + " is "+ sum/ 12 );

}
catch(IOException except)
{
outFile.println("IOException with site "+ dataSetName);
System.exit( 0 );
}
Free download pdf