Files - Page 2


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Page 2 of 2 FirstFirst 12
Results 16 to 18 of 18

Thread: Files

  1. #16
    Join Date
    Dec 2005
    Location
    New Jersey
    Posts
    290
    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();
        }
    }

  2. #17
    Join Date
    Sep 2005
    Posts
    74
    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.

  3. #18
    Join Date
    Dec 2005
    Location
    New Jersey
    Posts
    290
    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

  1. How to manage PAK files in vb programs?
    By Melawen in forum VB Classic
    Replies: 6
    Last Post: 04-17-2005, 06:56 AM
  2. XML files vs Database
    By Mansoor in forum Enterprise
    Replies: 2
    Last Post: 09-10-2002, 11:38 PM
  3. XML files vs Database
    By Mansoor in forum XML
    Replies: 8
    Last Post: 08-23-2002, 05:35 PM
  4. Replies: 0
    Last Post: 03-13-2001, 02:54 PM
  5. Replace In Files utility released
    By Ren Whitworth in forum vb.announcements
    Replies: 1
    Last Post: 04-29-2000, 03: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
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center