-
Files
Here is my code:
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];
try
{
// Prepare files
BufferedReader inFile = new BufferedReader(new FileReader(inFileName));
PrintWriter outFile = new PrintWriter(new FileWriter(outFileName));
// Read and write from input and output files
String testInfo = inFile.readLine();
outFile.println("Results " + testInfo);
outFile.println();
String num = inFile.readLine();
while (num != null)
{
doStats(num, inFile, outFile);
num = inFile.readLine();
}// while()
// Close the files
inFile.close();
outFile.close();
} catch (FileNotFoundException e){
display.setText("IOERROR: File NOT Found " + inFileName + "\n");
e.printStackTrace();
} catch (IOException e){
display.setText("IOERROR: " + e.getMessage() + "\n");
e.printStackTrace();
}// 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(String num, BufferedReader inFile,
PrintWriter outFile) throws IOException
{
int number;
int count = 0;
int sum = 0;
int countOfNums = 0;
int max = 0;
int min = 0;
while (count != 14)
{
number = Integer.parseInt(inFile.readLine());
sum += number;
countOfNums++;
if (max > number)
{
max = number;
} else {
max = 0;
}// if()
if (min < number)
{
min = number;
} else {
min = 0;
}// if()
count++;
}// while()
outFile.println("sum " + sum);
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
For some reason I'm getting an ArrayIndexOutOfBoundsException. I have a file called numbers.dat with a 13 numbers in it, and I'm trying to read from it, preform operations on the numbers, and output the results to both the JFrame and a new file. Can someone shed some light on what I'm doing wrong?
Last edited by Dark Rain; 01-21-2006 at 01:28 PM.
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
|
Development Centers
-- Android Development Center
-- Cloud Development Project Center
-- HTML5 Development Center
-- Windows Mobile Development Center
|