The code I'm writing reads in something like this from a file:
95 34 89 100 25 100 84 62 78 98 Mary Elizabeth Smith

And outputs this to the same filename with a different extension:
88.25 Mary Elizabeth Smith

I wrote a test program to test the calculation methods and they all work correctly. Some reason, the code is either not reading the info in the file, or failing to output the information to a file. It will make a file, but it is blank.

The program should read info from the file into an array, do its computing by dropping the 2 lowest grades and taking the average of that, and then outputting the average to a new file. Can you guys help any? Thanks!

/** Lab Grader
 * 
 * This program scans a file with a .lab extension full of grades for a certain student,
 * drops the two lowest grades, and then averages the grades and outputs them into a
 * file of the same name with an extension of .grd.
 * 
 * @author Justin Myers
 */



import java.util.*;
import java.io.*;
import java.util.Arrays;

public class LabGrader {
	
	public static int NUMBER = 10;
	public static int TOOSHORT = 3;
	public static int DROP = 2;
	
	
	public static void main(String[] args) {
		introMessage();
		userInterface();
	}
	
	/** Handels user input for filename and sends filename to input scanner.
	 * Also handles entry errors.
	 */
	public static void userInterface() {
		String filename = "";
		boolean quit = true;
		Scanner console = new Scanner(System.in);
		while (quit == true) {
			System.out.print("Enter input file name or Q to quit (must end with .lab): ");
			filename = console.next();
			if (filename.endsWith(".lab")) {
				getInputScanner(filename);
				getOutputPrintStream(filename, console);
			} else if (filename.toLowerCase().endsWith("q")) {
				System.out.println("Ending program.");
				quit = false;
			} else {
				System.out.println("File does not end with .lab");
			}
		}
	}
		
	/** Displays an intro message and instructions
	 */
	public static void introMessage() {
		System.out.println("Welcome to Lab Grader! When prompted, enter the input file name");
		System.out.println("where the lab grades for the course are located. Input file names");
		System.out.println("must end with .lab extension. An output file will be created with");
		System.out.println("the same name and a .grd extension Thank you for using Lab Grader!");
	}

	/** Gets input from file
	 * @param filename from user interface.
	 * @return input for file input from filename.
	 */
	public static Scanner getInputScanner(String filename) {
		Scanner input = null;
		while (input == null) {
			try {
				input = new Scanner(new File(filename));
				file = false;
			} catch (FileNotFoundException e) {
				System.out.println("File not found!");
			}
		}
		return input;
	}
	

	/** Checks to see whether the file named filename exists.
	 * If it does exist, asks the user if it is OK to overwrite the file.
	 * If the file does not exist OR if it is OK to overwrite an existing file,
	 * @param filename from userInterface.
	 * @param console to ask if user would like to overwrite.
	 * @return output PrintStream for the file; otherwise null is returned.
	 */
	public static PrintStream getOutputPrintStream(String filename, Scanner console) {
		PrintStream output = null;
		filename = (filename.substring(0,filename.indexOf(".")) + ".grd");
		try {
			output = new PrintStream(new File (filename));
		} catch (FileNotFoundException e) {   
			System.out.println("An error occured, ending program.");
			System.exit(1);
		}
		System.out.println("Creating " + filename + "...");
		return output;
	}
  
	/** Reads input to array and outputs info to new file.
	 * @param input from getInputScanner to read info in file.
	 * @param output from getOuputPrintStream to output info to new file.
	 */
	public static void processGrades(Scanner input, PrintStream output) {
    	int[] grades = new int [NUMBER];
    	int count = 0;
    	int[] processed = new int[NUMBER];
    	double average = 0;
    	
    	while (input.hasNext()) {
    		grades[count] = input.nextInt();
    		count++;
    	}
    	processed = dropLowest2Grades(grades);
		average = calculateAverage(processed);
    	output.print(average);
    }
  
	/** Drops 2 lowest grades from grades array.
	 * @throws IllegalArgumentException if grades length < 3.
	 * @param grades
	 * @return shortened array to be used for average.
	 */
	public static int[] dropLowest2Grades(int[] grades) {
		if (grades.length < TOOSHORT){
			throw new IllegalArgumentException("Must have at least 3 grades!");
		}
		Arrays.sort(grades);
		int[] shortened = new int [grades.length-DROP];
			for(int i = DROP; i < grades.length; i++) {
				shortened [i - DROP] = grades[i];
			}
			return shortened;
	}
  
	/** Gets average from grades array.
	 * @param grades
	 * @return average to output.
	 */
	public static double calculateAverage(int[] grades) {
		int sum = 0;
		double average = 0;
		for (int i = 0; i < grades.length; i++) {
			sum = sum + grades[i];
		}
		average = sum / grades.length;
		return average;
	}
}

in what part are you processing the grades??

I see that you have a processGrade method but you are not calling it anywhere..

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.