Results 1 to 1 of 1
Thread: Scan code for "errors"
- 03-14-2011, 01:09 AM #1
Member
- Join Date
- Jan 2011
- Posts
- 18
- Rep Power
- 0
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
-
offline inspection is not reporting "Hard coded String" errors
By g_madhu_krishna in forum IntelliJ IDEAReplies: 0Last Post: 09-17-2010, 08:02 AM -
question about so-called "memory consistency errors"
By gib65 in forum Threads and SynchronizationReplies: 2Last Post: 06-20-2010, 04:20 PM -
"Cannot find symbol" errors in Java
By 23Zone in forum New To JavaReplies: 1Last Post: 02-17-2010, 07:13 AM -
MoneyOut.println("It took you (whats wrong?>",year,"<WW?) years to repay the loan")
By soc86 in forum New To JavaReplies: 2Last Post: 01-24-2009, 06:56 PM -
the dollar sign "$", prints like any other normal char in java like "a" or "*" ?
By lse123 in forum New To JavaReplies: 1Last Post: 10-20-2008, 07:35 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks