Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: File Reading code doesn't work

  1. #1
    Junior Member
    Join Date
    Apr 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default File Reading code doesn't work

    My code needs to print certain results to a file if the user types something, and if they type nothing just print it to the screen. I wrote the code, and am including it below, but once my program prompts the user it doesn't do anything and I don't understand why. I know the code is long, but is fairly logically laid out. If anyone knows why please give me some insights, Thank You.
    import java.util.*;
    import java.io.*;
     
    public class Project4{
    	public static void main(String[] args)
    		throws FileNotFoundException {
    		Scanner input = new Scanner(System.in);
    		System.out.print("File Name:");
    		String fileName = input.nextLine();
    		Scanner console = new Scanner(new File(fileName));
    		int totalNumbers = console.nextInt();
    		double[] filearray;
    		filearray = new double[totalNumbers];
    		for (int i = 0; i < totalNumbers; i++)
    			filearray[i] = console.nextDouble();
    		System.out.print("Output [type a file name, or push return");
    		System.out.print(" to output to screen]: ");
    		String s = input.next();
    		System.out.print("There are " + totalNumbers);
    		System.out.println(" numbers in your input file");
    		if (s.length() == 0){
    			printResults(filearray);
    		} else {
    			writeResultsToFile(filearray);
    		}
    		Arrays.sort(filearray);
    		}
     
     
    	public static double calculateMax(double[] filearray){
    		double maximum = filearray[0];
    		for (int i = 1; i < filearray.length; i++){
    			if (filearray[i] > maximum){
    				maximum = filearray[i];
    				}
    			}
    			return maximum;
    			}
     
    	public static double calculateMin(double[] filearray){
    		double minimum = filearray[0];
    			for (int i = 1; i < filearray.length; i++){
    				if (filearray[i] < minimum){
    					minimum = filearray[i];
    					}
    				}
    				return minimum;
    				}		
     
     
    	public static double calculateMean(double[] filearray){
    		double sum = 0;
    			//calculates sum
    		for (int i = 0; i < filearray.length; i++){
    			sum = sum + filearray[i];
    			}
    			//calculates average
    			double mean = (sum / filearray.length);
    			return mean;
    	}
     
    	public static double calculateMedian(double[] filearray){
    		  int middle = (filearray.length+1)/2;
        if (filearray.length%2 == 1) {
            // Odd number of numbers -- return the middle one.
            return filearray[middle];
        } else {
           // Even number -- return average of middle two
           // Must cast to double before division.
           return (filearray[middle-1] + filearray[middle]) / 2.0;
        }
    }
     
    	public static double calculateMode(double[] filearray){
    		double maxValue = -1;
    		int maxCount = 0;
    		for (int i = 0; i < filearray.length; i++){
    			//counts number of times number is in array
    			int count = 0;
    			for(int j = 0; j < filearray.length; j++){
    				if(filearray[j] == filearray[i]){
    					count++;
    					}
    				}
    			//remember which was max
    			if (count > maxCount){
    				maxValue = filearray[i];
    				maxCount = count;
    			}
    		}
    	return maxValue;
    	}
     
    static double calculateStandardDeviation(double[] filearray, double meanOfArray){
            double squareOfDeviations = 0;
    		  for(int i = 0; i < filearray.length; i++){        
               double diff = filearray[i] - meanOfArray;
               squareOfDeviations = squareOfDeviations + (diff * diff);
     
            }
            double deviance = (squareOfDeviations/filearray.length); 
    		  return deviance;
     
            } 
    	public static void printResults(double[] filearray){
    		double maximumOfArray = calculateMax(filearray);
    		double minimumOfArray = calculateMin(filearray);
    		double modeOfArray = calculateMode(filearray);
    		double meanOfArray = calculateMean(filearray);
    		double medianOfArray = calculateMedian(filearray);
    		double standardDeviationOfArray = 
    			calculateStandardDeviation(filearray, meanOfArray);
    		System.out.println("Max: " + maximumOfArray);
    		System.out.println("Min: " + minimumOfArray);
    		System.out.println("Mode: " + modeOfArray);
    		System.out.println("Mean: " + meanOfArray);
    		System.out.println("Median " + medianOfArray);
    		System.out.println("Standard Deviation: " + standardDeviationOfArray);
    	}
     
    	public static void writeResultsToFile(double[] filearray)
    		throws FileNotFoundException{
    		PrintStream output = 
    		new PrintStream(new File("results.txt"));
    		double maximumOfArray = calculateMax(filearray);
    		double minimumOfArray = calculateMin(filearray);
    		double modeOfArray = calculateMode(filearray);
    		double meanOfArray = calculateMean(filearray);
    		double medianOfArray = calculateMedian(filearray);
    		double standardDeviationOfArray = 
    			calculateStandardDeviation(filearray, meanOfArray);
    		output.println("Max: " + maximumOfArray);
    		output.println("Min: " + minimumOfArray);
    		output.println("Mode: " + modeOfArray);
    		output.println("Mean: " + meanOfArray);
    		output.println("Median " + medianOfArray);
    		output.println("Standard Deviation: " + standardDeviationOfArray);
    	}
     
    }
    Last edited by copeg; April 25th, 2011 at 08:37 AM.


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: File Reading code doesn't work

    I suggest boiling the code down to an SSCCE to narrow the problem down to the minimal amount of code. There is a lot of code to wade through (and please use the code tags).

Similar Threads

  1. Scanner code won't work
    By r19ecua in forum What's Wrong With My Code?
    Replies: 6
    Last Post: April 21st, 2011, 04:49 PM
  2. File IO Compiles, but wont work?
    By StarKannon in forum File I/O & Other I/O Streams
    Replies: 5
    Last Post: February 2nd, 2011, 09:05 AM
  3. Timer code does not work!
    By mariapatrawala in forum What's Wrong With My Code?
    Replies: 1
    Last Post: December 10th, 2010, 10:03 AM
  4. WHY this code dont work?
    By sibbe in forum Java Theory & Questions
    Replies: 7
    Last Post: December 9th, 2010, 10:47 AM
  5. please tell me why this code does not work
    By amr in forum Java Theory & Questions
    Replies: 9
    Last Post: December 6th, 2010, 06:46 PM