Results 1 to 5 of 5
  1. #1
    koryvandell is offline Member
    Join Date
    Apr 2011
    Posts
    4
    Rep Power
    0

    Default Problem with code

    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.

    Java Code:
    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 Fubarable; 04-25-2011 at 03:37 AM. Reason: moderator edit: code tags added

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Moderator Edit: Code tags added to the post above to allow the code to retain its formatting and be readable.

    To the OP, in the future, to do this yourself, highlight your pasted code (please be sure that it is already formatted when you paste it into the forum; the code tags don't magically format unformatted code) and then press the code button, and your code will have tags.

    Another way to do this is to manually place the tags into your code by placing the tag [cod&#101;] above your pasted code and the tag [/cod&#101;] below your pasted code like so:

    Java Code:
    [cod&#101;]
      // your code goes here
      // notice how the top and bottom tags are different
    [/cod&#101;]
    Best of luck

  3. #3
    koryvandell is offline Member
    Join Date
    Apr 2011
    Posts
    4
    Rep Power
    0

    Default Thank You

    Thanks for letting me know. I will make sure to do that next time.

  4. #4
    Maximus-EVG is offline Member
    Join Date
    Apr 2011
    Location
    Canada!
    Posts
    30
    Rep Power
    0

    Default

    You should describe what you're trying to achieve in more detail, cause very few people like to guess.

    From my own guesswork, if you modify it as such, you can get user input (e.g. I typed 2 4 8, hit ENTER, then typed Done, but seems like you're not processing it correctly.

    Java Code:
            Scanner input = new Scanner(System.in);
            System.out.print("File Name:");
            //String fileName = input.nextLine();
            Scanner console = new Scanner(System.in);
            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 type \"Done\"");
            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.equals("Done")) {
                printResults(filearray);
            } else {
                writeResultsToFile(filearray);
            }
            Arrays.sort(filearray);
        }

  5. #5
    koryvandell is offline Member
    Join Date
    Apr 2011
    Posts
    4
    Rep Power
    0

    Default Clarification

    Before I wasnít able to get anything I just got 0.0 for most answers and mostly wrong answers. Now when it prompts the user to write a file or print on screen, and I hit enter, it doesnít do it, and instead just displays how many numbers were in the file and closes down. A better explanation of what Iím trying to do is this:

    I am trying to write a token-based, non-robust program that reads data from a file, calculates statistics on that data, and then outputs the statistics to either another file or to the screen, depending on what the user wants. Here is a sample run. The user requests data_in.txt be read and output goes to the screen:

    File name: data_in.txt
    Output [type a file name, or push return to output to screen]:
    There are 4 numbers in your input file.
    Max: 4.000
    Min: 1.000
    Mean: 2.250
    Median: 2.000
    Mode: 2.000
    Standard deviation: 1.258
    Notes:
    The first token will be an integer n > 1 indicating how many numbers are in the file. There will then be n more tokens, each a double, representing the data.
    The programís output should resemble the above example, including rounding to thousandthís.
    Data should be in an array with a method that returns the answers for each statistics

Similar Threads

  1. Replies: 0
    Last Post: 02-21-2011, 11:50 AM
  2. Replies: 9
    Last Post: 09-21-2010, 04:15 PM
  3. problem in my code
    By wannabe in forum New To Java
    Replies: 5
    Last Post: 04-12-2010, 04:38 AM
  4. Problem with code
    By jvasilj1 in forum New To Java
    Replies: 5
    Last Post: 02-02-2008, 08:34 AM
  5. Problem with my first code
    By paul in forum New To Java
    Replies: 2
    Last Post: 07-26-2007, 04:09 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •