If I were you (Dark Rain), I would combine the doStats method and the readFile method into one. Here's an application (not applet) that does what you want (I think..).
Code:
import java.io.*;
import java.util.*;
public class HelpFile {
static public void doStats() {
try {
PrintWriter pw = new PrintWriter(new FileWriter("outFile.txt"));
Scanner scanner = new Scanner(new File("test.txt"));
pw.close();
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("Program completed. Close window to exit program.");
}
static public void main(String[] args) {
doStats();
}
}
01-23-2006, 09:07 PM
Dark Rain
I would combine them, however my assignment statement states that they have to be in two seperate methods (I'm not even sure why). I edited my code a little, and I now have this:
public class FileStats extends JFrame
{
public static JTextArea display;
public FileStats()
{
super("Testing FileStats program");
display = new JTextArea();
getContentPane().add(display, "Center");
}// FileStats()
/* pre: args has command-line arguments
* post: both files in args have been closed;
* max, min, count, sum have been calculated
* calls: doStats
*/
public static void readFile(String[] args) throws IOException
{
// Get file name arguments from the command line as entered by the user
String inFileName = args[0];
String outFileName = args[1];
// Prepare files
BufferedReader inFile = new BufferedReader(new FileReader(inFileName));
PrintWriter outFile = new PrintWriter(new FileWriter(outFileName));
try
{
// Read and write from input and output files
String testInfo = inFile.readLine();
outFile.println("Results " + testInfo);
outFile.println();
doStats(inFile, outFile);
} catch (FileNotFoundException e){
display.setText("IOERROR: File NOT Found " + inFileName + "\n");
e.printStackTrace();
} catch (IOException e){
display.setText("IOERROR: " + e.getMessage() + "\n");
e.printStackTrace();
} finally {
// Close the files
inFile.close();
outFile.close();
}// end try/catch block
display.append("Program completed. Close window to exit program.\n");
}// readFile()
/* pre: num has a value
* post: max, min, count, sum have been set or reset
*/
public static void doStats(BufferedReader inFile,
PrintWriter outFile) throws IOException
{
String line;
int number;
int sum = 0;
int countOfNums = 0;
int max = 0;
int min = 0;
while( (line = inFile.readLine()) != null )
{
number = Integer.parseInt(line);
sum += number;
countOfNums++;
if (Integer.MAX_VALUE > number)
{
max = number;
} else {
max = 0;
}// if()
if (Integer.MIN_VALUE < number)
{
min = number;
} else {
min = 0;
}// if()
}// while()
public static void main(String[] args) throws IOException
{
FileStats fileStat = new FileStats();
readFile(args);
fileStat.setSize(400, 300);
fileStat.setVisible(true);
fileStat.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
}// main()
}// FileStats
If I include the space before the list of numbers, it works great. All I have to do is look up those java.lang.Math methods (min and max), and implement an average between them (that's what the number format import is for).
Thanks again for the help.
01-23-2006, 09:14 PM
destin
Look at my code. Do you see how i stored all the numbers in an ArrayList? I would recommend doing it this way, then in doStats you can just loop through the ArrayList rather than reading the file again.
also, Math.max() and Math.min() are very easy to implement (look at my code).
They both have two parameters; max returns the larger of the two, min returns the smaller of the two.