DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 15 of 18

Thread: Files

Threaded View

  1. #1
    Join Date
    Sep 2005
    Posts
    74

    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

  1. How to manage PAK files in vb programs?
    By Melawen in forum VB Classic
    Replies: 6
    Last Post: 04-17-2005, 05:56 AM
  2. XML files vs Database
    By Mansoor in forum Enterprise
    Replies: 2
    Last Post: 09-10-2002, 10:38 PM
  3. XML files vs Database
    By Mansoor in forum XML
    Replies: 8
    Last Post: 08-23-2002, 04:35 PM
  4. Replies: 0
    Last Post: 03-13-2001, 01:54 PM
  5. Replace In Files utility released
    By René Whitworth in forum vb.announcements
    Replies: 1
    Last Post: 04-29-2000, 02:54 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


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


Sponsored Links