Results 1 to 1 of 1
  1. #1
    phixion is offline Member
    Join Date
    Jan 2011
    Posts
    18
    Rep Power
    0

    Default Scan code for "errors"

    Hey.

    For my assignment, I had to write a file with 1000 numbers. Then, I had to read from that file to create another file with the numbers sorted. Lastly, I had to report the statistics of the sorted file onto the screen.

    Taking everyone's advice from two week ago, I separated 'functions' into their own methods instead of placing everything in Main.

    Is it too much to ask for someone to quickly scan my code and point out any advice they have, or areas where I could improve this program. I hate coming without specific questions but my professor rarely gives feedback, so I come here.

    I'm just trying to clean up any bad habits early.

    Thanks in advance

    *I will add comments later

    Java Code:
    import java.util.Scanner;
    import java.io.*;
    
    public class FileSort
    {
        public static void main (String[]args) throws IOException
        {
            createFile(createArray());
            double[] array = populateArray(readFile());
            
            Statistics stats = new Statistics(array);
            
            sortFile(array);
    
            printProperties(stats.GetStatistics());
            
            double[][] mode = stats.GetMode();
            printMode(mode);
        }
        
        
        public static double[] createArray()
        {
            double[] array = new double[1000];
            
                for(int x = 0; x<array.length; x++)
                {
                    double number = Math.random();
                    number = number * 100;
                    array[x] = (int)number;
                }
                
                return array;
        }
        
        public static void createFile(double[] array) throws IOException
        {
            File file = new java.io.File("Numbers.txt");
            PrintWriter output = new java.io.PrintWriter(file);
            
                for(int x = 0; x<array.length; x++)
                {
                    output.println((int)array[x]);
                }
                
            output.close();
        }
        
        public static double[] readFile() throws IOException
        {
            Scanner reader = new Scanner(new File("Numbers.txt"));
            PrintWriter output = new PrintWriter("SortedNumbers.txt");
           
            int input = 0;
            int size = 0;
            
                while (reader.hasNext())
                {
                    input = reader.nextInt();
                    output.println(input);
                    size++;
                }
            
            output.close();
            
            double[] array = new double[size];
            
            return array;
        }
        
        public static double[] populateArray(double[] file) throws IOException
        {
            Scanner reader = new Scanner(new File("SortedNumbers.txt"));
            
            int x = 0;
            
                while (reader.hasNext())
                {
                    file[x] = reader.nextInt();
                    x++;
                }
                
            reader.close();
            
            return file;
        }
        
        public static void sortFile(double[] numbers) throws IOException
        {
            Scanner reader = new Scanner(new File("SortedNumbers.txt"));
            PrintWriter output = new PrintWriter("SortedNumbers.txt");
            
                for (int x = 0; x<numbers.length; x++)
                {
                    output.println((int)numbers[x]);
                }
                
            output.close();
        }
        
        public static void printProperties(double[] properties)
        {
            System.out.println("Minimum: " + properties[0]);
            System.out.println("Maximum: " + properties[1]);
            System.out.println("Mean: " + properties[2]); 
            System.out.println("Median: " + properties[3]);
            System.out.println("Range: " + properties[4]);  
            System.out.println("Variance: " + properties[5]);  
            System.out.println("Standard Deviation: " + properties[6]);    
            System.out.println("Total: " + properties[7]);      
        }
        
        public static void printMode(double[][] mode)
        {
            System.out.println( "Mode: " + mode.length);
            
                for (int x = 0; x<mode.length ;x++)
                {
                    System.out.println("The number " + mode[x][0]+ " occurs " + (int)mode[x][1] + " times");
                }
                
        }
    }
    Last edited by phixion; 03-14-2011 at 01:32 AM.

Similar Threads

  1. offline inspection is not reporting "Hard coded String" errors
    By g_madhu_krishna in forum IntelliJ IDEA
    Replies: 0
    Last Post: 09-17-2010, 08:02 AM
  2. question about so-called "memory consistency errors"
    By gib65 in forum Threads and Synchronization
    Replies: 2
    Last Post: 06-20-2010, 04:20 PM
  3. "Cannot find symbol" errors in Java
    By 23Zone in forum New To Java
    Replies: 1
    Last Post: 02-17-2010, 07:13 AM
  4. Replies: 2
    Last Post: 01-24-2009, 06:56 PM
  5. Replies: 1
    Last Post: 10-20-2008, 07:35 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
  •