Here is my code:
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?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


Reply With Quote


Bookmarks