// ExamScores2.java
// Given an input text file ExamScores.txt
// containing exam scores, generates text file
// Curve.txt containing curved scores.
//
// Prints an error message if the file contains
// too many numbers to fit into the array.
//
// Note that the String line is declared and
// initialized before the loop which reads the
// input file. This is necessary so that the
// variable line can be tested later, after that
// loop.
/**
* Program which, given an input text file
* ExamScores.txt containing exam scores,
* generates text file Curve.txt containing
* curved scores.
*
* @author D. Nixon
*/
public class ExamScores2 {
/**
* Main method.
*
* @param args command-line arguments - none needed
*/
public static void main(String[] args)
{
final int MAX_NUMBER_OF_SCORES = 40;
final int columnWidth = 5;
final String inputFileName = "ExamScores.txt";
final String outputFileName = "Curve.txt";
TextFileInput in = new TextFileInput(inputFileName);
TextFileOutput out = new TextFileOutput(outputFileName);
// Read scores and determine maximum:
int[] scores = new int[MAX_NUMBER_OF_SCORES];
int maximumScore = 0;
int lengthFilled = 0;
String line = in.readLine(); // read first line in file
while ( lengthFilled < scores.length && line != null )
{
scores[lengthFilled] = Integer.parseInt(line);
maximumScore = Math.max(maximumScore, scores[lengthFilled]);
lengthFilled++;
line = in.readLine(); // read next line in file
} // for i
// Check to see if all the numbers in the file were read.
// If not, then the array wasn't big enough to hold them all.
// In that case, print an error message and quit.
if ( line != null ) // i.e. if end-of-file not reached
{
System.out.println("File contains too many numbers.");
System.out.println("This program can process only "
+ scores.length + " numbers.");
return;
} // if
// Determine how many points to curve up all scores:
int curveUp = 100 - maximumScore;
// Print original scores and curved scores
// to output text file:
for ( int i = 0; i < lengthFilled; i++ )
{
// Print original score in a right-justified column:
out.print(rightJustify(scores[i], columnWidth));
// Calculate curved score;
int curvedScore = scores[i] + curveUp;
// Print curved score in a right-justified column;
out.println(rightJustify(curvedScore, columnWidth));
} // for i
// Make sure the output finds its way to the file:
out.flush();
// Release files for re-use:
out.close();
in.close();
// Print message indicating successful completion:
System.out.println("File " + outputFileName
+ " has been created.");
} // method main(String[])
/**
* Generates a string consisting of a string
* representation of the specified integer
* padded with enough leading spaces to
* facilitate printing in a column with the
* specified width.
*
* @param numberToPrint number to be padded
* with leading spaces
* @param width column width
* @return string representation of
* <code>numberToPrint</code> padded
* with leading spaces
*/
private static String rightJustify(int numberToPrint,
int width)
{
String numberText = Integer.toString(numberToPrint);
for ( int i = numberText.length(); i < width; i++ )
numberText = " " + numberText;
return numberText;
} // method rightJustify
} // class ExamScores2