-
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.println("Results " + scanner.next());
pw.println();
ArrayList<Integer> numsInFile = new ArrayList<Integer>();
while (scanner.hasNextInt()) {
numsInFile.add(scanner.nextInt());
}
int sum = 0;
int max = numsInFile.get(0).intValue();
int min = max;
for (Integer i : numsInFile) {
max = Math.max(max, i.intValue());
min = Math.min(min, i.intValue());
sum += i.intValue();
}
pw.println("sum " + sum);
pw.println("count " + numsInFile.size());
pw.println("max " + max);
pw.println("min " + min);
pw.close();
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("Program completed. Close window to exit program.");
}
static public void main(String[] args) {
doStats();
}
}
-
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:
Code:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
import java.text.NumberFormat;
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()
outFile.println("sum " + sum + "\n");
display.append("sum " + sum + "\n");
outFile.println("count " + countOfNums);
display.append("count " + countOfNums + "\n");
outFile.println("max " + max);
display.append("max " + max + "\n");
outFile.println("min " + min);
display.append("min " + min + "\n");
}// doStats()
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.
-
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.
Math.max(5, 8) returns 8.
Similar Threads
-
By Melawen in forum VB Classic
Replies: 6
Last Post: 04-17-2005, 05:56 AM
-
By Mansoor in forum Enterprise
Replies: 2
Last Post: 09-10-2002, 10:38 PM
-
Replies: 8
Last Post: 08-23-2002, 04:35 PM
-
By Pat Meaney in forum Java
Replies: 0
Last Post: 03-13-2001, 01:54 PM
-
By René Whitworth in forum vb.announcements
Replies: 1
Last Post: 04-29-2000, 02:54 AM
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks